2016-12-14 58 views
0

我正在製作一個自定義相機,它允許用戶拍攝照片。我也希望用戶可以選擇從iOS圖庫中選擇照片。我想向他展示存儲在圖庫中的最後一張照片作爲他點擊打開圖庫並選擇圖像的按鈕,而不是圖庫圖標。如何挑選存儲在iOS畫廊中的最後一張圖片

我知道如何使用imagepickerdelegate來選擇圖像,但我不知道如何向他展示他可以點擊的按鈕中的最後一張圖片。

如何挑選畫廊中的最後一張照片?

+1

[Swift - 如何從照片庫中獲取最後拍攝的3張照片?]可能的副本(http://stackoverflow.com/questions/28259961/swift-how-to-get-last-taken-3-photos-from-照片庫) – Rob

回答

0

試試這個

import PhotosUI 



    { 

      var images: PHFetchResult<PHAsset>! 

      let allPhotosOptions = PHFetchOptions() 

      allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] 

      images = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: allPhotosOptions) 

    } 

let asset = images.object(at: index) 

imageManager.requestImage(for: asset, targetSize: thumbnailSize, contentMode: .aspectFill, options: nil, resultHandler: { image, _ in 

self.thumbnailImage = image 

}) 
+0

好像東西不見了 – user2238284

0

這是我從照片庫

@objc 
class Fetcher: NSObject, PHPhotoLibraryChangeObserver { 
    private var imageManager = PHCachingImageManager() 
    private var fetchResult: PHFetchResult! 

    override init() { 
     let options = PHFetchOptions() 
     options.sortDescriptors = [ NSSortDescriptor(key: "modificationDate", ascending: true) ] // or "creationDate" 
     self.fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: options) 
     super.init() 
     PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self) 
    } 

    deinit { 
     PHPhotoLibrary.sharedPhotoLibrary().unregisterChangeObserver(self) 
    } 

    func photoLibraryDidChange(changeInstance: PHChange) { 
     // Photos may call this method on a background queue; switch to the main queue to update the UI. 
     dispatch_async(dispatch_get_main_queue()) { [weak self] in 
      guard let sSelf = self else { return } 

      if let changeDetails = changeInstance.changeDetailsForFetchResult(sSelf.fetchResult) { 
       let updateBlock = {() -> Void in 
        self?.fetchResult = changeDetails.fetchResultAfterChanges 
       } 
      } 
     } 
    } 

    func requestLastCreateImage(targetSize:CGSize, completion: (UIImage) -> Void) -> Int32 { 
     let asset = self.fetchResult.lastObject as! PHAsset 
     let options = PHImageRequestOptions() 
     options.deliveryMode = .HighQualityFormat 
     return self.imageManager.requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: options) { (image, info) in 
      if let image = image { 
       completion(image) 
      } 
     } 
    } 

    func requestPreviewImageAtIndex(index: Int, targetSize: CGSize, completion: (UIImage) -> Void) -> Int32 { 
     assert(index >= 0 && index < self.fetchResult.count, "Index out of bounds") 
     let asset = self.fetchResult[index] as! PHAsset 
     let options = PHImageRequestOptions() 
     options.deliveryMode = .HighQualityFormat 
     return self.imageManager.requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: options) { (image, info) in 
      if let image = image { 
       completion(image) 
      } 
     } 
    } 

    func requestFullImageAtIndex(index: Int, completion: (UIImage) -> Void) { 
     assert(index >= 0 && index < self.fetchResult.count, "Index out of bounds") 
     let asset = self.fetchResult[index] as! PHAsset 
     self.imageManager.requestImageDataForAsset(asset, options: .None) { (data, dataUTI, orientation, info) -> Void in 
      if let data = data, image = UIImage(data: data) { 
       completion(image) 
      } 
     } 
    } 
} 

使用獲取的圖像幫手:

let fetcher = Fetcher() 
    let variable = fetcher.requestLastCreateImage(CGSize(width: 300, height: 300), completion: { image in 
     print(image) 
    }) 
相關問題