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

Objective c 如何从URL下载图像并将其保存到计算机?,objective-c,image,macos,download,Objective C,Image,Macos,Download,如何从URL下载图像,并使用Objective-C将其保存到计算机?到目前为止,我得到的是: NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; UIImage *imageFromURL = [self getImageFromURL:@"https://www.google.c

如何从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岁一样。

以下是将图像保存到文档目录的代码

-(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");
            }
}

这是我的解决方案

+(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_;
}

所以这是一个Mac OS X程序?是的,我正试图保存到Mac OS X计算机的硬盘上。这是使用iOS应用程序完成的(你将问题标记为iOS)还是使用Mac应用程序完成的?我没有这样做,其他人做了。我想在Mac上做这个。它甚至不是一个应用程序,我使用的是Mac命令终端应用程序。如果这不是一个iOS应用程序,那么为什么您发布的代码使用iOS特定的API,例如
UIImage
?非常感谢您的帮助,但我是一个完全的新手。有没有不使用自定义方法的方法?如果不是,那也没什么大不了的,等我弄清楚它是如何工作的,我还是会接受答案的。这个方法没什么大不了的,我只是在我的应用程序中为抽象创建了它。我多次使用这个方法,仅此而已。你可以在第一个方法中添加第二个方法代码。好吧,让我来解释一下,一旦开始,我会回来接受你的答案。查看我的更新我已经创建了一个方法代码。如果你有任何错误,请告诉我。请原谅,我是Objective-C的新手,所以我非常害怕尝试方法,直到我学会如何正确地实现它们。我现在开始尝试。谢谢你的跟进,我会尝试一下,一拿到就告诉你。