【问题标题】:How do I download an image from a URL and save it to my computer?如何从 URL 下载图像并将其保存到我的计算机?
【发布时间】:2013-11-15 04:01:39
【问题描述】:

如何从 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 岁一样向我解释这一点。

【问题讨论】:

  • 所以它是一个 Mac OS X 程序?
  • 是的,我正在尝试保存到 Mac OS X 计算机的硬盘。
  • 这是使用 iOS 应用程序完成(您使用 iOS 标记了问题)还是使用 Mac 应用程序完成?
  • 那不是我做的,是别人做的。我想在 Mac 上执行此操作。它甚至不是一个应用程序,我使用的是 Mac Command Terminal 应用程序。
  • 如果这不是 iOS 应用程序,那么为什么您发布的代码使用 iOS 特定的 API,例如 UIImage

标签: objective-c image macos download


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的帮助,但我完全是新手。有没有办法在不使用自定义方法的情况下做到这一点?如果没有,那也没什么大不了的,等我弄清楚它是怎么工作的,我还是会接受答案。
  • 这个方法没什么大不了的,我刚刚在我的应用程序中为抽象创建了它,因为我多次使用相同的方法,仅此而已。你可以在第一种方法中添加第二种方法代码,没关系。
  • 好吧,让我试一试,一旦我开始,我会回来接受你的回答。
  • 查看我的更新,我创建了一个方法代码。如果您遇到任何错误,请告诉我。
  • 对不起,我完全是 Objective-C 的新手,所以我非常害怕尝试方法,直到我学会了如何正确地实现它们。我现在开始尝试。感谢您的跟进,我会尝试一下,并在收到后立即通知您。
【解决方案2】:

这是我的解决方案!

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

【讨论】:

    猜你喜欢
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    相关资源
    最近更新 更多