Save images with phimagemanager to custom album?

后端 未结 5 1538
北荒
北荒 2020-11-28 04:14

I am making an app that takes pictures with AVFoundation and I want to save them to a custom album that I can then query and show in my app. (I\'d prefer to not have them in

5条回答
  •  广开言路
    2020-11-28 05:18

    This is how I do:

    At the top:

    import Photos
    
    var image: UIImage!
    var assetCollection: PHAssetCollection!
    var albumFound : Bool = false
    var photosAsset: PHFetchResult!
    var assetThumbnailSize:CGSize!
    var collection: PHAssetCollection!
    var assetCollectionPlaceholder: PHObjectPlaceholder!
    

    Creating the album:

    func createAlbum() {
        //Get PHFetch Options
        let fetchOptions = PHFetchOptions()
        fetchOptions.predicate = NSPredicate(format: "title = %@", "camcam")
        let collection : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
        //Check return value - If found, then get the first album out
        if let _: AnyObject = collection.firstObject {
            self.albumFound = true
            assetCollection = collection.firstObject as! PHAssetCollection
        } else {
            //If not found - Then create a new album
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                let createAlbumRequest : PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle("camcam")
                self.assetCollectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection
                }, completionHandler: { success, error in
                    self.albumFound = success
    
                    if (success) {
                        let collectionFetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([self.assetCollectionPlaceholder.localIdentifier], options: nil)
                        print(collectionFetchResult)
                        self.assetCollection = collectionFetchResult.firstObject as! PHAssetCollection
                    }
            })
        }
    }
    

    When saving the photo:

    func saveImage(){
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(self.image)
                let assetPlaceholder = assetRequest.placeholderForCreatedAsset
                let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset)
                albumChangeRequest!.addAssets([assetPlaceholder!])
                }, completionHandler: { success, error in
                    print("added image to album")
                    print(error)
    
                    self.showImages()
            })
        }
    

    Showing the images from that album:

    func showImages() {
            //This will fetch all the assets in the collection
    
            let assets : PHFetchResult = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: nil)
            print(assets)
    
            let imageManager = PHCachingImageManager()
            //Enumerating objects to get a chached image - This is to save loading time
            assets.enumerateObjectsUsingBlock{(object: AnyObject!,
                count: Int,
                stop: UnsafeMutablePointer) in
    
                if object is PHAsset {
                    let asset = object as! PHAsset
                    print(asset)
    
                    let imageSize = CGSize(width: asset.pixelWidth, height: asset.pixelHeight)
    
                    let options = PHImageRequestOptions()
                    options.deliveryMode = .FastFormat
    
                    imageManager.requestImageForAsset(asset, targetSize: imageSize, contentMode: .AspectFill, options: options, resultHandler: {(image: UIImage?,
                        info: [NSObject : AnyObject]?) in
                        print(info)
                        print(image)
                    })
                }
            }
    

提交回复
热议问题