2011-10-17 102 views
3

我正在为我的应用实施“在线图片搜索”功能。我需要的流程如下:如何将图片网址保存到照片库,然后使用保存的图片

1)获取用户想要使用

2)保存图像(通过URL)到手机的相册

3)图像的网址通过图像采集器控制器检索保存的图像并打开移动和缩放屏幕

4)使用从相册中检索的图像。

任何人都可以建议如何在获取图片网址后执行上述步骤?

回答

10

您可以保存在相册图像与此代码现在

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 
} 
+0

嗨,谢谢你的回应!你还可以告诉我如何将图像url转换为图像格式,以便我可以执行上面的代码将其写入相册中? – Zhen 2011-10-17 13:11:24