问题描述
所以我正在使用下面的代码从正常工作的库中获取所有图像:
So I am using below code to fetch all the images from library which is working fine :
func grabPhotos(){
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
if let fetchResults : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){
if fetchResults.count>0{
for i in 0..<fetchResults.count{
imgManager.requestImage(for: fetchResults.object(at: i), targetSize: CGSize(width:100, height: 100), contentMode: .aspectFill, options: requestOptions, resultHandler: {
image, error in
self.Galleryimages.append(image!)
print("array count is ",self.Galleryimages.count)
self.photoCollectionview.reloadData()
})
}
}
}
}
我正在UICollectionView中显示所有图像,但是无论何时单击任何缩略图图像,我都找不到任何获取原始图像的方法.当用户单击UICollectionView中填充的任何缩略图图像时,我想获取原始图像(全尺寸图像).
I am showing all the images in my UICollectionView, but I didn't find any way to get original image whenever clicking on any thumbnail image. I want to fetch the original image (full size image) when user clicks on any thumbnail image which is populated in UICollectionView.
谢谢.
推荐答案
加载缩略图.
做过多的事情后得到解决方案,可能对其他人有帮助.以下是执行此操作的步骤.
Got the solution after doing too much stuff, may be it can help to others. Below are the steps to do this.
步骤1:声明PHFetchResult对象
Step 1 : Declare object of PHFetchResult
var Galleryimages: PHFetchResult<PHAsset>!
第2步:使用以下代码从图库中获取结果:
Step 2 : Fetch results from gallery using below code:
func grabPhotos(){
Galleryimages = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
}
第3步:使用以下代码在UI(集合视图/表视图)中显示缩略图:
Step 3 : Show the thumbnail images in your UI (collectionview/Tableview) using below code :
let imageview = cell.viewWithTag(1) as! UIImageView
PHImageManager.default().requestImage(for: (Galleryimages?[indexPath.row])!, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: nil) { (image: UIImage?, info: [AnyHashable: Any]?) -> Void in
imageview.image = image
}
第4步:,最后使用以下代码获取完整尺寸的图像.
Step 4 : And finally get the full size image using below code.
let options = PHImageRequestOptions()
options.deliveryMode = .highQualityFormat
options.resizeMode = .exact
PHImageManager.default().requestImage(for: (Galleryimages[indexPath.row]), targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: options) { (image: UIImage?, info: [AnyHashable: Any]?) -> Void in
if let image = image {
//Use this originan image
}
}
这篇关于单击列表项时,获取列表中的缩略图图像和全尺寸图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!