Sim*_*iwi 106
此代码段将从相机胶卷(iOS 7及更低版本)获取最新图像:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
// Chooses the photo at the last index
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
// Stop the enumerations
*stop = YES; *innerStop = YES;
// Do something interesting with the AV asset.
[self sendTweet:latestPhoto];
}
}];
} failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(@"No groups");
}];
Run Code Online (Sandbox Code Playgroud)
iOS 8及以上版本:
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
[[PHImageManager defaultManager] requestImageForAsset:lastAsset
targetSize:self.photoLibraryButton.bounds.size
contentMode:PHImageContentModeAspectFill
options:PHImageRequestOptionsVersionCurrent
resultHandler:^(UIImage *result, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
[[self photoLibraryButton] setImage:result forState:UIControlStateNormal];
});
}];
Run Code Online (Sandbox Code Playgroud)
Lia*_*iam 20
iBrad的答案很棒,对我来说几乎是完美的.唯一的例外是它以原始方向返回图像(例如,倒置,-90°等).
为了解决这个问题,我只是改变了fullResolutionImage到fullScreenImage.
这里:
UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
Run Code Online (Sandbox Code Playgroud)
它现在有效.
isa*_*aac 10
iBrad的例子包括一个显然有效的iOS8片段,但我发现自己对他描述的返回类型感到困惑.这是一个抓取最后一个图像的片段,包括版本和大小要求的选项.
值得注意的是能够请求特定版本(原始,当前)和大小.在我的情况下,因为我希望将返回的图像应用于按钮,我请求它的大小和缩放以适合我正在应用它的按钮:
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
[[PHImageManager defaultManager] requestImageForAsset:lastAsset
targetSize:self.photoLibraryButton.bounds.size
contentMode:PHImageContentModeAspectFill
options:PHImageRequestOptionsVersionCurrent
resultHandler:^(UIImage *result, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
[[self photoLibraryButton] setImage:result forState:UIControlStateNormal];
});
}];
Run Code Online (Sandbox Code Playgroud)
感谢您对iBrad Apps的回答.
只是想指出当用户在他/她的照片卷上没有图像时的特殊情况的错误预防(我知道奇怪的情况):
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
//Check that the group has more than one picture
if ([group numberOfAssets] > 0) {
// Chooses the photo at the last index
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets] - 1)] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
[self.libraryButton setImage:latestPhoto forState:UIControlStateNormal];
}
}];
}
else {
//Handle this special case
}
Run Code Online (Sandbox Code Playgroud)
好吧,这是一个解决方案,如何从Swift 3家伙加载画廊的最后一个图像:
func loadLastImageThumb(completion: @escaping (UIImage) -> ()) {
let imgManager = PHImageManager.default()
let fetchOptions = PHFetchOptions()
fetchOptions.fetchLimit = 1
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]
let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
if let last = fetchResult.lastObject {
let scale = UIScreen.main.scale
let size = CGSize(width: 100 * scale, height: 100 * scale)
let options = PHImageRequestOptions()
imgManager.requestImage(for: last, targetSize: size, contentMode: PHImageContentMode.aspectFill, options: options, resultHandler: { (image, _) in
if let image = image {
completion(image)
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
如果你需要更快的速度,你也可以使用PHImageRequestOptions和设置:
options.deliveryMode = .fastFormat
options.resizeMode = .fast
Run Code Online (Sandbox Code Playgroud)
这是你在viewController中获取它的方式(你应该用你的类替换GalleryManager.manager):
GalleryManager.manager.loadLastImageThumb { [weak self] (image) in
DispatchQueue.main.async {
self?.galleryButton.setImage(image, for: .normal)
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅利亚姆的回答.fullScreenImage将返回适合您设备屏幕尺寸的缩放图像.要获得实际图像大小:
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
ALAssetOrientation orientation = [representation orientation];
UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:[representation scale] orientation:(UIImageOrientation)orientation];
Run Code Online (Sandbox Code Playgroud)
引用Apple的ALAssetRepresentation类参考fullResolutionImage:
要从CGImage创建正确旋转的UIImage对象,可以使用imageWithCGImage:scale:orientation:或initWithCGImage:scale:orientation :,传递方向和比例的值.
| 归档时间: |
|
| 查看次数: |
23687 次 |
| 最近记录: |