Noa*_*gev 9 photos ios phasset
我想从设备上的所有本地相册中检索所有照片.基本上设备上的所有照片是否本地标识符列表是唯一的?使用照片框架的最佳方法是什么?
我的问题不重复,因为另一个问题还涉及设备上没有的云资产和资产.检索图像的实际数据时,它在尝试获取同步时返回空数据.
\n\n\n我想检索设备上所有本地相册中的所有照片。\n 基本上设备上的所有照片
\n
编辑: fetchOptions.includeAssetSourceTypes = .typeUserLibrary参见下面的代码
\n\n我就是这样做的:
\n\nvar fetchResult: PHFetchResult<PHAsset>!\n\n...\n\nlet fetchOptions = PHFetchOptions() \nfetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]\nfetchOptions.includeAssetSourceTypes = .typeUserLibrary\n\nfetchResult = PHAsset.fetchAssets(with: fetchOptions)\nRun Code Online (Sandbox Code Playgroud)\n\n然后,如果我想将其用作 UImage 或缩略图(如果您希望将图像用作数据,则使用此let imageData: NSData = UIImagePNGRepresentation(myImage))我使用:
/* \n * From an asset to a UIImage, also can be used for thumbnail (if you change the :targetSize:)\n */\nfunc getAsset(_ asset: PHAsset) -> UIImage {\n\n //var thumbnail = UIImage()\n var imageData = Data()\n\n let options = PHImageRequestOptions()\n options.isSynchronous = true\n options.deliveryMode = .opportunistic\n options.resizeMode = .fast\n options.isNetworkAccessAllowed = false\n\n PHImageManager.default().requestImage(for: asset, targetSize: view.frame.size, contentMode: .aspectFill, options: options) { image, info in\n\n //thumbnail = image!\n imageData: NSData = UIImagePNGRepresentation(image)\n }\n //You can check if the UIImage is nil. If it is nil is an iCloud image\n //return thumbnail\n return imageData\n}\nRun Code Online (Sandbox Code Playgroud)\n\n您可能会根据您的需要自定义上述代码或添加更多功能!
\n\n上面的代码是使用Swift 3.1和Xcode 8.3.1编写和测试的
\n\n根据苹果文档
\n\n\n\n\nisNetworkAccessAllowed\n 一个布尔值,指定“照片”是否可以从 iCloud 下载请求的图像。\n 如果为 true,并且请求的图像未存储在本地设备上,“照片”将从 iCloud 下载图像。要获得下载\xe2\x80\x99s 进度的通知,请使用progressHandler 属性提供一个供Photos 在下载图像时定期调用的块。如果为 false(默认值),并且图像不在本地设备上,则结果处理程序\xe2\x80\x99s 信息字典中的 PHImageResultIsInCloudKey 值指示图像不可用,除非启用网络访问。
\n
使用 getAsset() 方法从资产中检索图像。有用
\n| 归档时间: |
|
| 查看次数: |
1756 次 |
| 最近记录: |