我正在为我的应用程序实现一个“在线图像搜索”功能。我需要的流程如下:
1)检索用户想要使用的图片url
2)将图片(通过URL)保存到手机相册中
3)通过图像拾取器控制器检索保存的图像,并打开移动和缩放屏幕
4)使用从相册中检索到的图像。
有没有人能告诉我如何在获得图像URL后执行上述步骤?
发布于 2011-10-17 21:05:27
你可以用这个代码保存相册中的图片
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
}https://stackoverflow.com/questions/7794126
复制相似问题