美文网首页iOS移动开发
SwiftyPhotos 一个非常好用的PhotoKit框架接口

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

作者: icetime17 | 来源:发表于2018-06-05 06:52 被阅读2次

    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
        }
    }
    

    相关文章

      网友评论

        本文标题:SwiftyPhotos 一个非常好用的PhotoKit框架接口

        本文链接:https://www.haomeiwen.com/subject/rygusftx.html