将从url加载的图像保存在iphone中



我已经从URL在UITableView中加载了图像。现在如何将这些图像保存在iPhone中。

下载图像时,使用适当的命名约定创建图像的缩略图并保存。

请提供一些保存图像的提示或方法

NSURL *url = [[NSURL alloc] initWithString:@"http://image.com"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

有关UIImageWriteToSavedPhotsAlbum的更多信息,您可以在此处找到

// data is the NSData you obtained
// from the NSURLConnection
UIImage* image = [[UIImage alloc] initWithData:data];
// path is an NSString containing the path
// to save the file, usually inside Documents, Cache, etc,
[image writeToFile:path];

*阅读苹果公司关于如何获取相关目录路径的文档。

这是从图像的weburl下载图像并将其保存在应用程序中的代码。

NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
 [NSURLConnection connectionWithRequest:request delegate:self];
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"image.jpg"];
 NSData *thedata = [NSData dataWithContentsOfURL:imageURL];
 [thedata writeToFile:localFilePath atomically:YES];

最新更新