单击列表项时获取列表中的缩略图和全尺寸图像
get thumbnail images in list and full size image when click on the item of list
所以我正在使用下面的代码从库中获取所有工作正常的图像:
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 中填充的任何缩略图时,我想获取原始图像(全尺寸图像)。
谢谢。
当您从 PHAsset 中获取时,您正在调整图像大小。所以使用
targetsize : PHImageManagerMaximumSize
从这个U可以得到原始尺寸的原始图像。对于您的收藏视图,您可以直接从中制作缩略图并显示图像。所以当用户点击缩略图时,现在你可以显示原始图像
请使用自动释放池进行内存管理。
for(PHAsset *asset in self.assets) {
// This autorelease pool seems good (a1)
autoreleasepool {
NSLog(@"started requesting image %i", i);
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:[self imageRequestOptions] resultHandler:^(UIImage *image, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
//you can add autorelease pool here as well (a2)
@autoreleasepool {
assetCount++;
NSError *error = [info objectForKey:PHImageErrorKey];
if (error) NSLog(@"Image request error: %@",error);
else {
NSString *imagePath = [appDelegate.docsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%i.png",i]];
NSData *imageData = UIImagePNGRepresentation(image);
if(imageData) {
[imageData writeToFile:imagePath atomically:YES];
[self.imagesArray addObject:imagePath];
}
else {
NSLog(@"Couldn't write image data to file.");
}
[self checkAddComplete];
NSLog(@"finished requesting image %i", i);
}
} //a2 ends here
});
}];
i++;
} // a1 ends here
}
加载缩略图。
做了很多事情后得到了解决方案,也许对其他人有帮助。以下是执行此操作的步骤。
步骤 1: 声明 PHFetchResult 对象
var Galleryimages: PHFetchResult<PHAsset>!
第 2 步: 使用以下代码从图库中获取结果:
func grabPhotos(){
Galleryimages = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
}
第 3 步: 使用以下代码在 UI (collectionview/Tableview) 中显示缩略图:
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 步: 最后使用以下代码获得全尺寸图像。
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
}
}
所以我正在使用下面的代码从库中获取所有工作正常的图像:
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 中填充的任何缩略图时,我想获取原始图像(全尺寸图像)。
谢谢。
当您从 PHAsset 中获取时,您正在调整图像大小。所以使用
targetsize : PHImageManagerMaximumSize
从这个U可以得到原始尺寸的原始图像。对于您的收藏视图,您可以直接从中制作缩略图并显示图像。所以当用户点击缩略图时,现在你可以显示原始图像
请使用自动释放池进行内存管理。
for(PHAsset *asset in self.assets) {
// This autorelease pool seems good (a1)
autoreleasepool {
NSLog(@"started requesting image %i", i);
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:[self imageRequestOptions] resultHandler:^(UIImage *image, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
//you can add autorelease pool here as well (a2)
@autoreleasepool {
assetCount++;
NSError *error = [info objectForKey:PHImageErrorKey];
if (error) NSLog(@"Image request error: %@",error);
else {
NSString *imagePath = [appDelegate.docsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%i.png",i]];
NSData *imageData = UIImagePNGRepresentation(image);
if(imageData) {
[imageData writeToFile:imagePath atomically:YES];
[self.imagesArray addObject:imagePath];
}
else {
NSLog(@"Couldn't write image data to file.");
}
[self checkAddComplete];
NSLog(@"finished requesting image %i", i);
}
} //a2 ends here
});
}];
i++;
} // a1 ends here
}
加载缩略图。
做了很多事情后得到了解决方案,也许对其他人有帮助。以下是执行此操作的步骤。
步骤 1: 声明 PHFetchResult 对象
var Galleryimages: PHFetchResult<PHAsset>!
第 2 步: 使用以下代码从图库中获取结果:
func grabPhotos(){
Galleryimages = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
}
第 3 步: 使用以下代码在 UI (collectionview/Tableview) 中显示缩略图:
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 步: 最后使用以下代码获得全尺寸图像。
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
}
}