首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将图像URL保存到图片库并随后使用保存的图像

问如何将图像URL保存到图片库并随后使用保存的图像
EN

Stack Overflow用户
提问于 2011-10-17 21:01:41
回答 1查看 5.8K关注 0票数 4

我正在为我的应用程序实现一个“在线图像搜索”功能。我需要的流程如下:

1)检索用户想要使用的图片url

2)将图片(通过URL)保存到手机相册中

3)通过图像拾取器控制器检索保存的图像,并打开移动和缩放屏幕

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

有没有人能告诉我如何在获得图像URL后执行上述步骤?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-17 21:05:27

你可以用这个代码保存相册中的图片

代码语言:javascript
复制
    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
    }
}

现在,为了在另一个线程中执行代码,我会像这样编写代码

代码语言:javascript
复制
// load data in new thread
[NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];

此方法可以在代码、按钮或任何其他UIKit控件中的任何位置使用。然后,你将需要一些方法来完成繁重的工作。

代码语言:javascript
复制
- (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 
}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7794126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档