2016-07-26 93 views

回答

0
import Photos 

class MyVC: UIViewController 
{ 
var images:NSMutableArray! // Array for storing images 


func fetchPhotos() { 
    images = NSMutableArray() 
    self.fetchPhotoAtIndexFromEnd(0) 
} 


func getImageAtIndex(index:Int) { 

    let imgManager = PHImageManager.defaultManager() 


    var requestOptions = PHImageRequestOptions() 
    requestOptions.synchronous = true // if just return thumbnail not img 

    // optionally sort the images by creation date 
    var fetchOptions = PHFetchOptions() 
    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)] 

    if let fetchResult: PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) { 


     if fetchResult.count > 0 { 
      imgManager.requestImageForAsset(fetchResult.objectAtIndex(fetchResult.count - 1 - index) as PHAsset, targetSize: view.frame.size, contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler: { (image, _) in 

       // Add img to images array 
       self.images.addObject(image) 


       if index + 1 < fetchResult.count { 
        self.fetchPhotoAtIndexFromEnd(index + 1) 
       } else { 

        println("Completed array: \(self.images)") 
       } 
      }) 
     } 
    } 
} 
+0

什麼是fetchPhotoAtIndexFromEnd? – mafioso

相關問題