我正在为我的应用实现“在线图片搜索”功能。我需要的流程如下:
1) 检索用户想要使用的图像 url
2) 保存图片(通过URL)到手机相册
3) 通过图像选择器 Controller 检索保存的图像并打开移动和缩放屏幕
4) 使用从相册中检索到的图像。
谁能告诉我在获取图片 URL 后如何执行上述步骤?
最佳答案
您可以使用此代码将图像保存在相册中
UIImageWriteToSavedPhotosAlbum(yourImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error != NULL)
{
// handle error
}
else
{
// handle ok status
}
}
现在为了在另一个线程中执行代码,我会编写这样的代码
// load data in new thread
[NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];
您可以在代码、按钮或任何其他 UIKit 控件的任何位置使用此方法。然后,您将需要能够完成艰苦工作的方法。
- (void)downloadImage
{
// network animation on
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// create autorelease pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// save image from the web
UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"your_image_address.com"]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
[self performSelectorOnMainThread:@selector(imageDownloaded) withObject:nil waitUntilDone:NO ];
[pool drain];
}
- (void)imageDownloaded
{
// network animation off
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// do whatever you need to do after
}
关于objective-c - 如何将图像 URL 保存到照片库并随后使用保存的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7794126/