如何从URL下载图像并将其保存到计算机中?

时间:2013-11-15 04:01:40

标签: objective-c image macos download

如何从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调用才能执行此操作。像我一样向我解释这个。

2 个答案:

答案 0 :(得分: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");
            }
}

答案 1 :(得分: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_;
}