iOS照片框架 [英] IOS Photos framework

查看:288
本文介绍了iOS照片框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to retrieve all the photos from all the local albums on the device. Basically all the photos that are on the device Will the list of local identifiers be unique ? What is the best approach for this using the photos framework.

我的问题不是重复的,因为另一个问题也谈到了云资产和不在设备上的资产.检索图像的实际数据时,尝试获取同步时它会返回空数据.

My question is not duplicate since the other question talks about also cloud assets and assets that are not on the device. When retrieving actual data of the image it returns null data when trying to fetch synchronize.

推荐答案

我想从所有本地相册中检索所有照片 设备.基本上是设备上的所有照片

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或缩略图(也希望将图像作为Data使用此let imageData: NSData = UIImagePNGRepresentation(myImage)),则使用:

Then if I want to use it as UImage or thumbnail (also if you want the image as Data use this let imageData: NSData = UIImagePNGRepresentation(myImage)) I use:

/* 
 * 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
}

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

You will probably customize the above code or add more features based on your needs!

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

The above code is written and tested using Swift 3.1 and Xcode 8.3.1

根据Apple的文档

isNetworkAccessAllowed 一个布尔值,它指定照片是否可以从iCloud下载请求的图像. 如果为true,并且请求的图像未存储在本地设备上,则Photos从iCloud下载图像.要获得下载进度的通知,请使用progressHandler属性提供一个块,在下载图像时,Photos会定期调用该块.如果为false(默认设置),并且图像不在本地设备上,则结果处理程序的信息字典中的PHImageResultIsInCloudKey值表示该图像不可用,除非您启用网络访问.

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()方法从资产中检索图像.可以

Use the getAsset() method for retrieving the image from the asset. It works

这篇关于iOS照片框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆