objective-c - 如何将图像 URL 保存到照片库并随后使用保存的图像

标签 objective-c ios image url uiimagepickercontroller

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

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/

相关文章:

ios - 使用@IBDesignable 更改标签字体

ios - 台风将属性注入(inject)新对象

css:如何定位p a img

c# - .NET Core 2.2下System.Drawing中找不到Image类

iphone - 使用CGContextDrawPDFPage绘制PDF页面异常泄漏

iOS:是否可以将注释放在 map 的中心并在注释始终保持在中心的情况下移动 map ?

objective-c - NSManagedObjectContext 通过通知中心问题在多个线程上保存/合并

iphone - 如何在运行时确定二进制图像架构?

objective-c - 用手指触摸移动时防止 View 对象跳跃

html - 倒 Angular/圆 Angular 图像 HTML