【发布时间】:2016-12-14 14:14:16
【问题描述】:
我正在制作一个自定义相机,它允许用户拍摄照片。我还希望用户可以选择从 iOS 画廊中选择照片。而不是画廊图标,我想向他展示存储在画廊中的最后一张照片,作为他将单击以打开画廊并选择图像的按钮。
我知道如何使用 imagepickerdelegate 来选择图像,但我不知道如何在他可以点击的按钮中向他展示画廊的最后一张照片。
如何挑选图库中的最后一张照片?
【问题讨论】:
标签: ios swift avfoundation
我正在制作一个自定义相机,它允许用户拍摄照片。我还希望用户可以选择从 iOS 画廊中选择照片。而不是画廊图标,我想向他展示存储在画廊中的最后一张照片,作为他将单击以打开画廊并选择图像的按钮。
我知道如何使用 imagepickerdelegate 来选择图像,但我不知道如何在他可以点击的按钮中向他展示画廊的最后一张照片。
如何挑选图库中的最后一张照片?
【问题讨论】:
标签: ios swift avfoundation
试试这个
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
})
【讨论】:
这是我从照片库中获取图片的助手
@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)
})
【讨论】: