简体   繁体   English

如何从Swift中的ALAssetRepresentation获取低分辨率图像或缩略图

[英]How to get a low res image, or Thumbnail from the ALAssetRepresentation in Swift

I am working with the ALAssetLibrary to get the images from my camera roll for a custom view I am making. 我正在使用ALAssetLibrary从我的相机胶卷中获取图像,以获得我正在制作的自定义视图。 Doing so was pretty simple: 这样做非常简单:

library.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
        (group: ALAssetsGroup?, stop: UnsafeMutablePointer<ObjCBool>) in
        if group != nil {
            group!.setAssetsFilter(ALAssetsFilter.allPhotos())
            var indexSet = NSIndexSet(indexesInRange: NSMakeRange(0, group!.numberOfAssets() - 1))
            group!.enumerateAssetsAtIndexes(indexSet, options: nil, usingBlock: {
                (result: ALAsset!, index: Int, stop: UnsafeMutablePointer<ObjCBool>) in
                if (result != nil) {
                    var alAssetRapresentation: ALAssetRepresentation = result.defaultRepresentation()
                    var url = alAssetRapresentation.url()
                    var iRef = alAssetRapresentation.fullResolutionImage().takeUnretainedValue()
                    var image = UIImage(CGImage: iRef)
                }
            })
        }
        }) { (NSError) -> Void in
    }

All this works great in the simulator. 所有这一切在模拟器中都很有效。 However on a device, getting the fullResolutionImage() proves to be way to memory intensive on the device and causes crashes. 但是在设备上,获取fullResolutionImage()证明是设备上的内存密集型并导致崩溃。 I figured that makes perfect sense, dozens of high res images all loaded into memory is a terrible idea, so I'd like to scale the quality back and only show the thumbnail of the image. 我认为这很有意义,所有加载到内存中的数十个高分辨率图像都是一个糟糕的想法,所以我想缩小质量,只显示图像的缩略图。 Only issue is that there is no simple way I've found to get a thumbnail from the AlAssetRepresentation . 唯一的问题是我找不到从AlAssetRepresentation获取缩略图的简单方法。

I am trying to work with the CGImageSourceCreateThumbnailAtIndex to create a thumbnail but have been very confused on how to do this. 我正在尝试使用CGImageSourceCreateThumbnailAtIndex来创建缩略图,但是对于如何执行此操作却非常困惑。

Any help is much appreciated! 任何帮助深表感谢!

Here's an example (there might be some minor compilation issues, depending what version of Swift you are using): 这是一个例子(可能会有一些小的编译问题,取决于你使用的是什么版本的Swift):

let src = CGImageSourceCreateWithURL(url, nil)
let scale = UIScreen.mainScreen().scale
let w = // desired display width, multiplied by scale
let d : [NSObject:AnyObject] = [
    kCGImageSourceShouldAllowFloat : true,
    kCGImageSourceCreateThumbnailWithTransform : true,
    kCGImageSourceCreateThumbnailFromImageAlways : true,
    kCGImageSourceThumbnailMaxPixelSize : w
]
let imref = CGImageSourceCreateThumbnailAtIndex(src, 0, d)
let im = UIImage(CGImage: imref, scale: scale, orientation: .Up)!

However, note that you should not be using ALAssetsLibrary any longer. 但请注意,您不应再使用ALAssetsLibrary。 It is deprecated in iOS 9. Switch to Photo Kit, and welcome to the modern world! 它在iOS 9中已弃用。切换到Photo Kit,欢迎来到现代世界! Now you can call PHImageManager.defaultManager().requestImageForAsset , which allows you to supply a targetSize for the desired image. 现在,您可以调用PHImageManager.defaultManager().requestImageForAsset ,它允许您为所需的图像提供targetSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从ALAssetRepresentation获取“修饰过的”图像 - Get the “retouched” image from ALAssetRepresentation 从ALAssetRepresentation生成自定义缩略图 - Generating custom thumbnail from ALAssetRepresentation 如何从ALAssetRepresentation获得正确旋转的UIImage? - How to get a correctly rotated UIImage from an ALAssetRepresentation? 如何从Swift中的UIPickerViewController中获取视频的缩略图? - How to get thumbnail image of video picked from UIPickerViewController in Swift? Swift 中 ALAssetRepresentation CGImage 的内存泄漏 - Memory leak from ALAssetRepresentation CGImage in Swift PHImageManager.requestImageForAsset在iOS Swift中返回低分辨率图像 - PHImageManager.requestImageForAsset returns low-res image in iOS swift 如何从Swift中的.m3u8格式视频网址获取缩略图? - How to get thumbnail image from .m3u8 formate video url in Swift? 如何从 UIImagePickerViewcontroller 获取缩略图和原始图像? - How to get thumbnail and Original image from UIImagePickerViewcontroller? 如何获取共享照片流ALAsset的ALAssetRepresentation? - How to get a ALAssetRepresentation of a shared photostream ALAsset? 如何从录制的视频-AVFoundation获取帧作为图像(图像作为缩略图) - How to get an frame as an Image from the Recorded Video -AVFoundation (Image as a thumbnail)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM