get thumbnail images in list and full size image when click on the item of list

3.4k views Asked by At

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()
      })
      }

    }
  }
}

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.

Thank you.

2

There are 2 answers

0
Sumeet Purohit On BEST ANSWER

To load thumbnail image.

Got the solution after doing too much stuff, may be it can help to others. Below are the steps to do this.

Step 1 : Declare object of PHFetchResult

var Galleryimages: PHFetchResult<PHAsset>!

Step 2 : Fetch results from gallery using below code:

func grabPhotos(){

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

}

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
}

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
  }
}
8
Jitendra Modi On

You are resizing images when U fetch from PHAsset. So use

targetsize : PHImageManagerMaximumSize

From this U can get original image with its original size. and for your collectionview you can direclty make thumbnail from it and display images. So when user taps on thumbnail, now you can show original image

Please use autoreleasepool for memory management.

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
}