2017-02-15 73 views
0

的项目所以我用下面的代码来获取所有从库,做工精细的图像:得到列表和全尺寸的图像缩略图时,点击列表

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中填充的任何缩略图图像时,我想获取原始图像(全尺寸图像)。

谢谢。

+0

意味着你想要做什么? –

+0

请看我编辑的问题,我实际上想要在用户点击任何缩略图时获取原始大小的图像。 –

+0

你不是在一个数组内存储所有的图像吗?不能得到原始图像是什么意思? – KrishnaCA

回答

2

要加载缩略图图像。

做了太多事情后得到了解决方案,可能是对别人有帮助。以下是执行此操作的步骤。

步骤1:PHFetchResult

申报对象
var Galleryimages: PHFetchResult<PHAsset>! 

步骤2:

func grabPhotos(){ 

Galleryimages = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil) 

}

步骤3::取使用以下代码从图库结果在您的用户界面中显示缩略图图像(coll ectionview /泰伯维)使用下面的代码:

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 
    } 
} 
0

当您从PHAsset中获取图像时,您正在调整图像大小。所以使用

targetsize : PHImageManagerMaximumSize 

从这个U可以得到它的原始大小的原始图像。并为你的collectionview你可以direclty从它缩略图并显示图像。所以当用户点击缩略图时,现在你可以显示原始图像

请使用autoreleasepool进行内存管理。

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 
} 
+0

您正确地使用'PHImageManagerMaximumSize'作为参数。但是,仅为缩略图加载全尺寸图片并不值得。您最好只在点击图片缩略图并需要显示全尺寸图片时才请求全尺寸图片。 – Abizern

+0

@JeckyModi如果我使用PHImageManagerMaximumSize作为目标大小,我的应用程序将因为内存压力而崩溃。这是我用200 x 200作为目标大小,以避免崩溃。你能解释你答案的完整方式吗? –

+0

@SumeetPurohit你可以使用autoreleasepool按照答案http://stackoverflow.com/questions/33274791/high-memory-usage-looping-through-phassets-and-calling-requestimageforasset请看你想要的答案 –