【发布时间】:2011-12-09 07:07:30
【问题描述】:
我正在为我的应用实施“在线图片搜索”功能。我需要的流程如下:
1) 获取用户想要使用的图片 url
2) 将图片(通过 URL)保存到手机相册
3) 通过图像选择器控制器检索保存的图像并打开移动和缩放屏幕
4) 使用从相册中检索到的图像。
任何人都可以告诉我在获取图像 URL 后如何执行上述步骤吗?
【问题讨论】:
标签: objective-c ios image url uiimagepickercontroller
我正在为我的应用实施“在线图片搜索”功能。我需要的流程如下:
1) 获取用户想要使用的图片 url
2) 将图片(通过 URL)保存到手机相册
3) 通过图像选择器控制器检索保存的图像并打开移动和缩放屏幕
4) 使用从相册中检索到的图像。
任何人都可以告诉我在获取图像 URL 后如何执行上述步骤吗?
【问题讨论】:
标签: objective-c ios image url uiimagepickercontroller
您可以使用此代码将图像保存在相册中
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
}
【讨论】: