有没有一种方法可以将我的照片库的最新照片添加到uiimageview中?我已经尝试了几乎所有内容,但没有任何效果。
斯威夫特3
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [SortDescriptor(key: "creationDate", ascending: true)]
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
let last = fetchResult.lastObject
if let lastAsset = last {
let options = PHImageRequestOptions()
options.version = .current
PHImageManager.default().requestImage(
for: lastAsset,
targetSize: imageView.bounds.size,
contentMode: .aspectFit,
options: options,
resultHandler: { image, _ in
DispatchQueue.main.async {
self.imageView.image = image
}
}
)
}
斯威夫特2
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
let last = fetchResult.lastObject
if let lastAsset = last {
let options = PHImageRequestOptions()
options.version = .Current
options.deliveryMode = .HighQualityFormat
PHImageManager.defaultManager().requestImageForAsset(
lastAsset as! PHAsset,
targetSize: imageView.bounds.size,
contentMode: .AspectFit,
options: options,
resultHandler: { image, _ in
dispatch_async(dispatch_get_main_queue(), {
self.imageView.image = image
})
}
)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句