SwiftyPhotos 一個非常好用的PhotoKit框架介面

克里斯_萊昂納多_羅斯柴爾德發表於2018-06-07

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
    }
}
複製程式碼

相關文章