2016-07-25 87 views

回答

3

斯威夫特3

let fetchOptions = PHFetchOptions() 
fetchOptions.sortDescriptors = [SortDescriptor(key: "creationDate", ascending: true)] 

let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) 

let last = fetchResult.lastObject 

if let lastAsset = last { 
    let options = PHImageRequestOptions() 
    options.version = .current 

    PHImageManager.default().requestImage(
    for: lastAsset, 
    targetSize: imageView.bounds.size, 
    contentMode: .aspectFit, 
    options: options, 
    resultHandler: { image, _ in 
     DispatchQueue.main.async { 
     self.imageView.image = image 
     } 
    } 
) 
} 

斯威夫特2

@IBOutlet weak var imageView: UIImageView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let fetchOptions = PHFetchOptions() 
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] 

    let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions) 

    let last = fetchResult.lastObject 

    if let lastAsset = last { 
     let options = PHImageRequestOptions() 
     options.version = .Current 
     options.deliveryMode = .HighQualityFormat 

     PHImageManager.defaultManager().requestImageForAsset(
      lastAsset as! PHAsset, 
      targetSize: imageView.bounds.size, 
      contentMode: .AspectFit, 
      options: options, 
      resultHandler: { image, _ in 
       dispatch_async(dispatch_get_main_queue(), { 
        self.imageView.image = image 
       }) 
      } 
     ) 
    } 
} 
+0

將如何代碼看起來像雨燕2?對不起,但我從未在 – mafioso

+0

之前使用過Swift,我將在一分鐘內添加Swift 2版本。 –

+0

我已經添加了Swift 2版本。 –