如何选择存储在 iOS 库中的最后一张图片
Posted
技术标签:
【中文标题】如何选择存储在 iOS 库中的最后一张图片【英文标题】:How to pick the last image stored in the iOS gallery 【发布时间】:2016-12-14 14:14:16 【问题描述】:我正在制作一个自定义相机,它允许用户拍摄照片。我还希望用户可以选择从 ios 画廊中选择照片。而不是画廊图标,我想向他展示存储在画廊中的最后一张照片,作为他将单击以打开画廊并选择图像的按钮。
我知道如何使用 imagepickerdelegate 来选择图像,但我不知道如何在他可以点击的按钮中向他展示画廊的最后一张照片。
如何挑选图库中的最后一张照片?
【问题讨论】:
Swift - how to get last taken 3 photos from photo library?的可能重复 【参考方案1】:试试这个
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
)
【讨论】:
好像东西不见了【参考方案2】:这是我从照片库中获取图片的助手
@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)
)
【讨论】:
以上是关于如何选择存储在 iOS 库中的最后一张图片的主要内容,如果未能解决你的问题,请参考以下文章