标签:
今天做聊天,领导说对方给我发一个图片,我要保存到本地,IOS的UIKit Framework提供了UIImageWriteToSavedPhotosAlbum方法对图像进行保存,该方法会将image保存至用户的相册中:
上代码:
void UIImageWriteToSavedPhotosAlbum ( UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo );
参数说明:
image : 需要保存的图片对象
id : 响应方法对象
SEL : 选择方法
步骤:
/** * 将图片添加到本地相册 */ - (void)addImageViewTolocal{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_currentItem.imageURL]]; UIImage *myImage = [UIImage imageWithData:data]; [self saveImageToPhotos:myImage]; }
步骤二:
- (void)saveImageToPhotos:(UIImage*)savedImage { UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); }
步骤三:
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo { NSString *msg = nil ; if(error != NULL){ msg = @"保存图片失败" ; }else{ msg = @"保存图片成功" ; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; }
这样就可以实现将图片保存至本地相册;
标签:
原文地址:http://www.cnblogs.com/LzwBlog/p/5703590.html