Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift中UIImageWriteToSavedPhotosAlbum之后的图像URL_Ios_Xcode_Swift_Ios8 - Fatal编程技术网

Ios Swift中UIImageWriteToSavedPhotosAlbum之后的图像URL

Ios Swift中UIImageWriteToSavedPhotosAlbum之后的图像URL,ios,xcode,swift,ios8,Ios,Xcode,Swift,Ios8,我用相机拍照后,用以下代码保存图像: UIImageWriteToSavedPhotosAlbum(image, self,"image:didFinishSavingWithError:contextInfo:", nil) 这会将图像保存在摄影机卷中。另外,我想将这个保存的图像的URL保存在核心数据中。是否可以检索到的URL(或路径),以便我只需将此URL而不需要将完整图像保存在核心数据中?在阅读了大量用Objective编写的答案后,我终于想出了如何使用Xcode 6.2在Swift中编

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

UIImageWriteToSavedPhotosAlbum(image, self,"image:didFinishSavingWithError:contextInfo:", nil)

这会将图像保存在摄影机卷中。另外,我想将这个保存的图像的URL保存在核心数据中。是否可以检索到的URL(或路径),以便我只需将此URL而不需要将完整图像保存在核心数据中?

在阅读了大量用Objective编写的答案后,我终于想出了如何使用Xcode 6.2在Swift中编写此内容:

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

这是我的密码

保存图像:

    let imageData = NSData(data:UIImagePNGRepresentation(pickedimage))
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    var docs: String = paths[0] as! String
    let fullPath = docs.stringByAppendingPathComponent("yourNameImg.png")
    let result = imageData.writeToFile(fullPath, atomically: true)

    print(fullPath)
获取图像:

    let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
    let nsUserDomainMask    = NSSearchPathDomainMask.UserDomainMask
    if let paths            = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
    {
        if paths.count > 0
        {
            if let dirPath = paths[0] as? String
            {
                let readPath = dirPath.stringByAppendingPathComponent("yourNameImg.png")
                let image    = UIImage(contentsOfFile: readPath)

                UploadImagePreview.image = image
            }
        }
    }
我们可以尝试使用照片框架来获取最后保存的图像

    var fetchOptions: PHFetchOptions = PHFetchOptions()

    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

    var fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)

    if (fetchResult.firstObject != nil) {

        var lastAsset: PHAsset = fetchResult.lastObject as! PHAsset

        PHImageManager.defaultManager().requestImageForAsset(lastAsset, targetSize: self.UploadImagePreview.bounds.size, contentMode: PHImageContentMode.AspectFill, options: PHImageRequestOptions(), resultHandler: { (result, info) -> Void in

            self.UploadImagePreview.image = result

        })

    }

可能的重复:不幸的是,该答案是用ObjectiveC书写的。我需要Swift编程语言中的这个……不要忘记导入语句:
import assetsbrary
;)注意:从iOS 9开始,此API已被弃用。正如Tys在“而是使用PHPhotoLibrary和“performChanges”方法将图像保存到设备”中的评论中所说,“如果我们只需要一个对象,我们可以使用
fetchOptions.fetchLimit=1
。此外,顺序错误,应为
升序:false