3

I am making a custom camera, which allows the user to capture a photo. I also want the user to have an option of picking a photo form the iOS gallery. Instead of a gallery icon, I want to show him the last photo stored in the gallery as the button he will click on to open the gallery and choose the image.

I know how to use the imagepickerdelegate to pick the image, but I do not know how I can show him the last photo of the gallery in a button he can click on.

How to I pick out the last photo in the gallery?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
charak
  • 187
  • 3
  • 15
  • 1
    Possible duplicate of [Swift - how to get last taken 3 photos from photo library?](http://stackoverflow.com/questions/28259961/swift-how-to-get-last-taken-3-photos-from-photo-library) – Rob Dec 14 '16 at 14:49

2 Answers2

1

try this

import PhotosUI



    {

            var images: PHFetchResult<PHAsset>!

            let allPhotosOptions = PHFetchOptions()

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

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

    }

more

let asset = images.object(at: index)

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

self.thumbnailImage = image

})
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
0

This is my helper for fetching image from photo library

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

Usage:

    let fetcher = Fetcher()
    let variable = fetcher.requestLastCreateImage(CGSize(width: 300, height: 300), completion: { image in
      print(image)
    })
Zept
  • 1,158
  • 1
  • 10
  • 14