将图像从 PHAsset 加载到 Imageview
Load Images From PHAsset into an Imageview
我是 Swift 的新人。在下面的代码中,它检索照片并将它们放入一个数组中。现在我想在图像视图中显示它们。我该怎么做?
我的意思是,例如,如何在图像视图中显示数组的元素。
var list :[PHAsset] = []
PHPhotoLibrary.requestAuthorization { (status) in
switch status
{
case .authorized:
print("Good to proceed")
let fetchOptions = PHFetchOptions()
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
print(allPhotos.count)
allPhotos.enumerateObjects({ (object, count, stop) in
list.append(object)
})
print("Found \(allPhotos.count) images")
case .denied, .restricted:
print("Not allowed")
case .notDetermined:
print("Not determined yet")
}
另一个问题是:当我调用这个函数时,它似乎是异步执行的。我的意思是调用函数后的代码行将提前执行。这是因为 requestAuthorization 吗?
试试这个:imageView.image = convertImageFromAsset(list[0])
func convertImageFromAsset(asset: PHAsset) -> UIImage {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var image = UIImage()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
image = result!
})
return image
}
希望对您有所帮助。
你可以这样做:-
创建一个 PHAsset 类型的空数组:-
fileprivate var imageAssets = [PHAsset]()
通过调用此函数获取所有图像:-
func fetchGallaryResources(){
let status = PHPhotoLibrary.authorizationStatus()
if (status == .denied || status == .restricted) {
self.showAlert(cancelTitle: nil, buttonTitles:["OK"], title: "Oops", message:"Access to PHPhoto library is denied.")
return
}else{
PHPhotoLibrary.requestAuthorization { (authStatus) in
if authStatus == .authorized{
let imageAsset = PHAsset.fetchAssets(with: .image, options: nil)
for index in 0..<imageAsset.count{
self.imageAssets.append((imageAsset[index]))
}
}
}
请求图像像这样:-
let availableWidth = UIScreen.main.bounds.size.width
let availableHeight = UIScreen.main.bounds.size.height
循环或单个从 imageAssets 中取出图像 :-
PHImageManager.default().requestImage(for: imageAssets[0], targetSize: CGSize(width : availableWidth, height : calculatedCellWidth), contentMode: .default, options: nil, resultHandler: { (image, info) in
requestedImageView.image = image
})
我是 Swift 的新人。在下面的代码中,它检索照片并将它们放入一个数组中。现在我想在图像视图中显示它们。我该怎么做?
我的意思是,例如,如何在图像视图中显示数组的元素。
var list :[PHAsset] = []
PHPhotoLibrary.requestAuthorization { (status) in
switch status
{
case .authorized:
print("Good to proceed")
let fetchOptions = PHFetchOptions()
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
print(allPhotos.count)
allPhotos.enumerateObjects({ (object, count, stop) in
list.append(object)
})
print("Found \(allPhotos.count) images")
case .denied, .restricted:
print("Not allowed")
case .notDetermined:
print("Not determined yet")
}
另一个问题是:当我调用这个函数时,它似乎是异步执行的。我的意思是调用函数后的代码行将提前执行。这是因为 requestAuthorization 吗?
试试这个:imageView.image = convertImageFromAsset(list[0])
func convertImageFromAsset(asset: PHAsset) -> UIImage {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var image = UIImage()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
image = result!
})
return image
}
希望对您有所帮助。
你可以这样做:-
创建一个 PHAsset 类型的空数组:-
fileprivate var imageAssets = [PHAsset]()
通过调用此函数获取所有图像:-
func fetchGallaryResources(){
let status = PHPhotoLibrary.authorizationStatus()
if (status == .denied || status == .restricted) {
self.showAlert(cancelTitle: nil, buttonTitles:["OK"], title: "Oops", message:"Access to PHPhoto library is denied.")
return
}else{
PHPhotoLibrary.requestAuthorization { (authStatus) in
if authStatus == .authorized{
let imageAsset = PHAsset.fetchAssets(with: .image, options: nil)
for index in 0..<imageAsset.count{
self.imageAssets.append((imageAsset[index]))
}
}
}
请求图像像这样:-
let availableWidth = UIScreen.main.bounds.size.width
let availableHeight = UIScreen.main.bounds.size.height
循环或单个从 imageAssets 中取出图像 :-
PHImageManager.default().requestImage(for: imageAssets[0], targetSize: CGSize(width : availableWidth, height : calculatedCellWidth), contentMode: .default, options: nil, resultHandler: { (image, info) in
requestedImageView.image = image
})