ios保存gif到相册_iOS 相册和网络图片的存取

iOS 相册和网络图片的存取

保存 UIImage 到相册

UIKit

UIKit 中一个古老的方法,Objective-C 的形式

void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);

保存完成后,会调用 completionTarget 的 completionSelector。如果 completionTarget 不为空,completionTarget 必须实现以下方法

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;

Objective-C 的写法

- (void)saveImage:(UIImage *)image {

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

if (error) {

// Fail

} else {

// Success

}

}

Swift 的写法

func saveImage(_ image: UIImage) {

UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

}

func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: AnyObject) {

if error == nil {

// Success

} else {

// Fail

}

}

Photos framework

iOS 8 开始,可以用 Photos framework。PHAssetChangeRequest 的类方法可以保存 UIImage

class func creationRequestForAsset(from image: UIImage) -> Self

编辑相册需要在 PHPhotoLibrary 的闭包中进行,有两种方法

func performChanges(_ changeBlock: @escaping () -> Void, completionHandler: ((Bool, Error?) -> Void)? = nil)

func performChangesAndWait(_ changeBlock: @escaping () -> Void) throws

以上两种方法,分别是异步和同步执行。一般用第一种异步执行的方法,不会阻塞主线程。

func saveImage(_ image: UIImage) {

PHPhotoLibrary.shared().performChanges({

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值