简体   繁体   English

从URL下载图像并存储在设备中(可以是iPod中的Photos文件夹),而不是应用程序中

[英]Download an image from URL and store in device (can be Photos folder in iPod), NOT IN APPLICATION

I have an issue about downloading image from URL. 我有一个关于从URL下载图像的问题。 After checking, I realized that I could download and stored image into application (connect iPod to MAC and open this application, I can find this image which has just downloaded from server), but this image hasn't appeared in Photos folder of device (my iPod). 检查后,我意识到可以下载图像并将其存储到应用程序中(将iPod连接到MAC并打开该应用程序,我可以找到刚刚从服务器下载的图像),但是该图像未出现在设备的Photos文件夹中(我的iPod)。 Following is my code: 以下是我的代码:

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg"]];
    [NSURLConnection connectionWithRequest:request delegate:self];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"pkm.jpg"];
    NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg"]];
    [thedata writeToFile:localFilePath atomically:YES];

Main task of my application is to download an image from server (via URL) and store image into Photo folder (or other folder that I can create), after that I use this image to set background for iPod. 我的应用程序的主要任务是从服务器(通过URL)下载图像并将图像存储到Photo文件夹(或我可以创建的其他文件夹)中,然后使用该图像设置iPod的背景。

Has anyone ever seen this case ? 有人见过这种情况吗? Please give me any ideas to resolve this issue. 请给我任何解决此问题的想法。

Thanks anyway, 不管怎么说,还是要谢谢你,

Ryan. 瑞安

You will need to save the image to the photo album, you can use the ALAssetsLibrary classes for this. 您将需要将图像保存到相册,您可以为此使用ALAssetsLibrary类。 They might be a bit more steep to learn but very powerful. 他们可能学起来有些陡峭,但功能非常强大。

Or use the very easy: UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); 或使用非常简单的方法: UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); method. 方法。 This will just save the image to the Photo library. 这只会将图像保存到照片库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM