1
有沒有辦法將我的照片庫的最新照片添加到uiimageview?我嘗試了幾乎所有的東西,但沒有任何作用如何使用Swift將照片庫的最新照片添加到uiimageview?
有沒有辦法將我的照片庫的最新照片添加到uiimageview?我嘗試了幾乎所有的東西,但沒有任何作用如何使用Swift將照片庫的最新照片添加到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
})
}
)
}
}
將如何代碼看起來像雨燕2?對不起,但我從未在 – mafioso
之前使用過Swift,我將在一分鐘內添加Swift 2版本。 –
我已經添加了Swift 2版本。 –