将较大的图像保存到照片库会返回错误,并且无法使用Swift在iOS中保存

时间:2019-07-12 18:19:56

标签: swift uiimage phphotolibrary userinfo

编辑-发现我需要保存一个在其中设置了cgImage而不是ciImage的UIImage。在使用带有cgImage设置的UIImage之后,它将保存得很好。

我的应用程序中有一项新功能,可让用户保存从该应用程序拍摄的照片的高质量版本。这将调用保存功能,可以正确保存照片的1280x720px版本。但是由于某种原因,这对于保存4096x2304px的图像不起作用。

在完成处理程序中,状态为false,错误为_userInfo = nil。我不确定如何给它_userInfo或它应该是什么。

我的功能是:

func save(image: UIImage , completion : @escaping () -> ()) {
    if assetCollection == nil {
        return // if there was an error upstream, skip the save
    }

    PHPhotoLibrary.shared().performChanges({
        let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
        let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
        let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
        let enumeration: NSArray = [assetPlaceHolder!]

        if self.assetCollection.estimatedAssetCount == 0
        {
            albumChangeRequest!.addAssets(enumeration)
        }
        else {
            albumChangeRequest!.insertAssets(enumeration, at: [0])
        }

    }, completionHandler: { status , error in
        completion( )
    })
}

PHPhotoLibrary对象是:

open class PHPhotoLibrary : NSObject {

    open class func shared() -> PHPhotoLibrary


    open class func authorizationStatus() -> PHAuthorizationStatus

    open class func requestAuthorization(_ handler: @escaping (PHAuthorizationStatus) -> Void)


    // handlers are invoked on an arbitrary serial queue
    // Nesting change requests will throw an exception
    open func performChanges(_ changeBlock: @escaping () -> Void, completionHandler: ((Bool, Error?) -> Void)? = nil)

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


    open func register(_ observer: PHPhotoLibraryChangeObserver)

    open func unregisterChangeObserver(_ observer: PHPhotoLibraryChangeObserver)
}

0 个答案:

没有答案