6

如何从 URL 下载图像,并使用 Objective-C 将其保存到计算机?这是我到目前为止得到的:

NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

UIImage *imageFromURL = [self getImageFromURL:@"https://www.google.com/images/srpr/logo11w.png"];

[self saveImage:imageFromURL withFileName:@"Google Logo" ofType:@"png" inDirectory:documentsDirectoryPath];

UIImage *imageFromWeb = [self loadImage:@"Google Logo" ofType:@"png" inDirectory:documentsDirectoryPath];

Xcode 抱怨 UIIMage,试图用 NSImage 替换。它还抱怨未声明的标识符“self”。

我还需要进行 HTTP 调用来执行此操作。像我 5 岁一样向我解释这一点。

4

2 回答 2

6

这是将图像保存到文档目录的代码。

-(void)saveImagesInLocalDirectory
{
        NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *imgName = @"image.png";
            NSString *imgURL = @"www.example.com/image/image.png";
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSString *writablePath = [documentsDirectoryPath stringByAppendingPathComponent:imgName];

            if(![fileManager fileExistsAtPath:writablePath]){
                // file doesn't exist
                NSLog(@"file doesn't exist");
                if (imgName) {
                    //save Image From URL
                    [self getImageFromURLAndSaveItToLocalData:imgName fileURL:imgURL inDirectory:documentsDirectoryPath];
                }
            }
            else{
                // file exist
                NSLog(@"file exist");
            }
}

-(void) getImageFromURLAndSaveItToLocalData:(NSString *)imageName fileURL:(NSString *)fileURL inDirectory:(NSString *)directoryPath {
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];

    NSError *error = nil;
    [data writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]] options:NSAtomicWrite error:&error];

    if (error) {
        NSLog(@"Error Writing File : %@",error);
    }else{
        NSLog(@"Image %@ Saved SuccessFully",imageName);
    }
}

这是一种方法代码..

-(void)saveImagesInLocalDirectory
{
        NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *imgName = @"image.png";
            NSString *imgURL = @"www.example.com/image/image.png";
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSString *writablePath = [documentsDirectoryPath stringByAppendingPathComponent:imgName];

            if(![fileManager fileExistsAtPath:writablePath]){
                // file doesn't exist
                NSLog(@"file doesn't exist");
                    //save Image From URL
                    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]];

                    NSError *error = nil;
                    [data writeToFile:[documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imgName]] options:NSAtomicWrite error:&error];

                   if (error) {
                          NSLog(@"Error Writing File : %@",error);
                   }else{
                          NSLog(@"Image %@ Saved SuccessFully",imgName);
                   }
            }
            else{
                // file exist
                NSLog(@"file exist");
            }
}
于 2013-11-15T04:26:55.627 回答
0

这是我的解决方案!

+(BOOL)downloadMedia :(NSString*)url_ :(NSString*)name{
    NSString *stringURL = url_;
    NSURL  *url = [NSURL URLWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    if ( urlData )
    {
        NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,name];
        [urlData writeToFile:filePath atomically:YES];
        return YES;
    }
    return NO;
}

+(UIImage*)loadMedia :(NSString*)name{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:name];
    UIImage *img_ = [UIImage imageWithContentsOfFile:getImagePath];
    return img_;
}
于 2015-05-16T16:08:33.617 回答