gpt4 book ai didi

IOS 照片框架

转载 作者:技术小花猫 更新时间:2023-10-29 11:22:17 25 4
gpt4 key购买 nike

我想从设备上的所有本地 相册中检索所有照片。基本上设备上的所有照片本地标识符列表是否唯一?使用照片框架的最佳方法是什么。

我的问题没有重复,因为另一个问题也涉及云 Assets 和不在设备上的 Assets 。当检索图像的实际数据时,它会在尝试获取同步时返回空数据。

最佳答案

I want to retrieve all the photos from all the local albums on the device. Basically all the photos that are on the device

编辑:fetchOptions.includeAssetSourceTypes = .typeUserLibrary见下面的代码

我是这样做的:

var fetchResult: PHFetchResult<PHAsset>!

...

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.includeAssetSourceTypes = .typeUserLibrary

fetchResult = PHAsset.fetchAssets(with: fetchOptions)

然后如果我想将它用作 UImage 或缩略图(如果你想将图像用作数据,请使用此 let imageData: NSData = UIImagePNGRepresentation(myImage))我使用:

/* 
* From an asset to a UIImage, also can be used for thumbnail (if you change the :targetSize:)
*/
func getAsset(_ asset: PHAsset) -> UIImage {

//var thumbnail = UIImage()
var imageData = Data()

let options = PHImageRequestOptions()
options.isSynchronous = true
options.deliveryMode = .opportunistic
options.resizeMode = .fast
options.isNetworkAccessAllowed = false

PHImageManager.default().requestImage(for: asset, targetSize: view.frame.size, contentMode: .aspectFill, options: options) { image, info in

//thumbnail = image!
imageData: NSData = UIImagePNGRepresentation(image)
}
//You can check if the UIImage is nil. If it is nil is an iCloud image
//return thumbnail
return imageData
}

您可能会根据需要自定义上述代码或添加更多功能!

以上代码是使用 Swift 3.1 和 Xcode 8.3.1 编写和测试的

根据 Apple 文档

isNetworkAccessAllowed A Boolean value that specifies whether Photos can download the requested image from iCloud. If true, and the requested image is not stored on the local device, Photos downloads the image from iCloud. To be notified of the download’s progress, use the progressHandler property to provide a block that Photos calls periodically while downloading the image. If false (the default), and the image is not on the local device, the PHImageResultIsInCloudKey value in the result handler’s info dictionary indicates that the image is not available unless you enable network access.

使用 getAsset() 方法从 Assets 中检索图像。有效

关于IOS 照片框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43114112/

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