SwiftyPhotos 一个非常好用的PhotoKit框架接口

本文介绍了一个针对PhotoKit框架的封装库SwiftyPhotos,解决了PhotoKit接口使用不便的问题。通过SwiftyPhotos可以轻松实现相册操作、照片选择、图片保存与删除等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SwiftyPhotos 是针对PhotoKit框架做的一个封装, 主要解决PhotoKit接口使用困难的问题.

Authorization Status

SwiftyPhotos.shared.reloadAll { (isPhotoAuthrized) in
    if isPhotoAuthrized {
        if let allPhotosAlbum = SwiftyPhotos.shared.allPhotoAlbums.first {
            self.photoAlbum = allPhotosAlbum
        }

        DispatchQueue.main.async {
            self.setupUI()
        }
    } else {
        print("please allow photo authorization status")
        DispatchQueue.main.async {
            let alertVC = UIAlertController(title: "Fail to visit iPhone photo album", message: nil, preferredStyle: .alert)
            let goSettings = UIAlertAction(title: "Go to Settings", style: .default, handler: { (alertAction) in
                print("go to settings")
                if let url = URL(string: UIApplicationOpenSettingsURLString) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            })
            let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            alertVC.addAction(goSettings)
            alertVC.addAction(cancel)
            self.present(alertVC, animated: true, completion: nil)
        }
    }
}
复制代码

Album

PhotoAlbumModel contains what you need for a album.

Check if album is existing or create album with name.

if let _ = SwiftyPhotos.shared.photoAlbumWithName("SwiftyPhotos") {
} else {
    _ = SwiftyPhotos.shared.createAlbum("SwiftyPhotos")
}
复制代码

Create a PhotoAssetsView, and use PhotoAlbumsViewDelegate to handle the photo selection action.

let view = PhotoAssetsView(frame: frame, photoAlbum: self.photoAlbum, isKeepingPhotoRatio: false, cellCountOfLine: 3, cellOffset: 2.0)
复制代码
public protocol PhotoAlbumsViewDelegate: class {
    func PhotoAlbumsViewDidSelectPhotoAlbum(_ photoAlbum: PhotoAlbumModel)
}
复制代码

Photo

PhotoAssetModel contains what you need for a photo asset.

Request thumbnail for a photo.

public var photoAsset: PhotoAssetModel! {
    didSet {
        self.imageRequestID = self.photoAsset.requestThumbnail(resultHandler: { (image, info) in
            DispatchQueue.main.async {
                if let info = info {
                    if let requestID = info[PHImageResultRequestIDKey] as? NSNumber {
                        if requestID.int32Value == self.imageRequestID {
                            self.thumbnail.image = image
                        }
                    }
                }
            }
        })
    }
}
复制代码

Request a photo from iCloud.

if self.photoAsset.isInCloud {
    print("photo in icloud")
    self.photoAsset.requestAvailableSizeImageInCloud { [weak self] (image, info) in
        if let image = image {
            self?.imageView.image = image
        }
    }
    self.photoAsset.requestMaxSizeImageInCloud(resultHandler: { [weak self] (image, info) in
        if let image = image {
            self?.imageView.image = image
        }
    }) { [weak self] (progress, error, stop, info) in
        self?.progressOfDownloadingInCloud = progress
        print("downloading progress of icloud photo: \(String(progress))")
    }
} else {
    self.photoAsset.requestMaxSizeImage { [weak self] (image, info) in
        if let image = image {
            self?.imageView.image = image
        }
    }
}
复制代码

Save or delete image

_ = SwiftyPhotos.shared.saveImage(image, intoAlbum: "SwiftyPhotos", withLocation: nil) { (isImageSaved, nil) in
    print("image saved: \(isImageSaved)")
}


_ = SwiftyPhotos.shared.deleteAsset(self.photoAsset) { (isAssetDeleted, error) in
    print("asset deleted: \(isAssetDeleted)")
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
        self.dismiss(animated: true, completion: nil)
    })
}            
复制代码

ZoomImageView

let v = ZoomImageView(frame: self.view.bounds)
self.photoAsset.requestMaxSizeImage { [weak self] (image, info) in
    if let image = image {
        self?.zoomImageView.image = image
    }
}
复制代码

转载于:https://juejin.im/post/5b1894da6fb9a01e643e2365

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值