在Swift中UIImageWriteToSavedPhotosAlbum之后的图像的URL

Man*_*ody 13 xcode ios swift ios8

在我用相机拍照后,我使用以下代码保存图像:

UIImageWriteToSavedPhotosAlbum(image, self,"image:didFinishSavingWithError:contextInfo:", nil)
Run Code Online (Sandbox Code Playgroud)

这样可以将图像保存在相机胶卷中.此外,我想将URL保存到Core Data中保存的图像.是否可以检索URL(或路径),以便我只需要保存此URL而不是Core Data中的完整图像?

Man*_*ody 6

在阅读了ObjectivC中写的很多答案之后,我终于想出了如何用Xcode 6.2在Swift中编写这个:

ALAssetsLibrary().writeImageToSavedPhotosAlbum(image.CGImage, orientation: ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!,
            completionBlock:{ (path:NSURL!, error:NSError!) -> Void in
                println("\(path)")
        })
Run Code Online (Sandbox Code Playgroud)

  • NB从iOS 9开始,该API已被弃用.正如Tys在http://stackoverflow.com/a/4458451/171144的评论中所说,"而是使用PHPhotoLibrary和"performChanges"方法将图像保存到设备." (3认同)