gpt4 book ai didi

swift - 对相机胶卷上图像文件的文件访问(iOS 12.1.4、Swift 4.2、Xcode 10.1)

转载 作者:行者123 更新时间:2023-11-30 10:49:23 25 4
gpt4 key购买 nike

我试图在使用内置相机拍照后计算 JPEG 文件(例如 MD5)的文件哈希值。我可以对图像数据创建摘要,但这与实际文件哈希不同。查看 Photo 模块及其 PHAsset 类,我没有找到一种方法来检索实际文件路径 (URL),然后将其提供给 MD5Digest。也许可以通过实现完成监听器功能,在将图像保存到相机胶卷后立即捕获文件URL。有人尝试过吗?

使用completionHandler更新了代码,但我仍然需要找到URL。

 @IBOutlet weak var imageHash: UILabel!

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

let orgPic = info[UIImagePickerController.InfoKey.originalImage] as? UIImage

UIImageWriteToSavedPhotosAlbum(orgPic!, self, #selector(imageSaved(_:didFinishSavingWithError:contextInfo:)), nil)

}

@objc func imageSaved(_ img: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "\(img.ciImage?.url)", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}

// the img.ciImage.url is "nil" unfortunately

最佳答案

所以你的主要座右铭是比较 2 个图像文件的 MD5。这就是我尝试过的方法,也许会对您有所帮助

;TLDR - 库中保存的文件和实际文件与我不匹配

 public func saveImgToLocal(_ img: UIImage,
onComplete: @escaping () -> Void) {

var blockPlaceholder: PHObjectPlaceholder?
var changeRequest: PHAssetChangeRequest?

PHPhotoLibrary.shared().performChanges({
changeRequest = PHAssetChangeRequest.creationRequestForAsset(from: img)
blockPlaceholder = changeRequest?.placeholderForCreatedAsset
}, completionHandler: { (saved, error) in
//_saved_ is of type bool, you can use it to check whether image is saved
//Here we would fetch the asset using our saved placeholder
let fetchOptions = PHFetchOptions()
let fetchResult:PHFetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [blockPlaceholder!.localIdentifier], options: fetchOptions)
if let asset = fetchResult.firstObject {
//Method to convert asset to Actual image
self.getAsset(asset: asset)
}
onComplete()

})
}

这就是我将图像保存到本地的方法

这就是我从本地获取图像的方式

func getAsset(asset: PHAsset)  {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var resultImage = UIImage()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
resultImage = result!
let url = info!["PHImageFileURLKey"] as! NSURL
//Image created using the file URL.
let image = UIImage.init(contentsOfFile: url.path!)
//I even compared the resultImage instead of the image from the file
if UIImagePNGRepresentation(image!) == UIImagePNGRepresentation(self.image!) {
print("Same image")
}
})

}

因此,如果您只想取回保存的图像,这是可以做到的,但我不确定图像是否按原样保存在照片库中。呵呵:-)

关于swift - 对相机胶卷上图像文件的文件访问(iOS 12.1.4、Swift 4.2、Xcode 10.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54997588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com