2017-02-15 41 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請看你想要的答案 –

相關問題