Swift调整照片库中的图像(ALAssets)的缩放比例

时间:2015-03-13 19:23:43

标签: swift uiimageview image-resizing alassetslibrary cgimage

我正在寻找解决方案如何显示保存在照片库中的图像的缩略图。我知道有缩略图的方法但结果太小(并且对于相反的fullScreen尺寸太大)。我想设置我想要的缩略图的自定义大小。我试图寻找任何解决方案,但我认为这太复杂了。现在我有这些显示拇指的方法:

func setFromAlbum(album:ALAssetsGroup) {
    //self.imageView.image = UIImage(CGImage:album.posterImage().takeUnretainedValue())

    album.enumerateAssetsWithOptions(NSEnumerationOptions.Reverse, usingBlock: {
        (asset: ALAsset!, index: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
        if ((asset) != nil) // first non-nil element will be the recent asset
        {
            var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
            self.imageView.image = UIImage(CGImage: assetRep.fullScreenImage().takeUnretainedValue())
            stop.initialize(true)
        }
    })
}

这是显示照片的缩略图:

func setFromAsset(asset:ALAsset) {
    let assetRep: ALAssetRepresentation = asset.defaultRepresentation()
    let image = UIImage(CGImage: assetRep.fullScreenImage().takeUnretainedValue())
    self.photoImageView.image = image
}

我试过这个但没有成功:

func setFromAsset(asset:ALAsset) {

    let imageOptions:[NSObject: AnyObject] = [
        kCGImageSourceThumbnailMaxPixelSize : 20
    ]
    let assetRep: ALAssetRepresentation = asset.defaultRepresentation()
    let imageRef = assetRep.CGImageWithOptions(imageOptions).takeUnretainedValue()
    let image = UIImage(CGImage: imageRef)
    self.photoImageView.image = image
}

我在字典中尝试了更多具有不同值的键,但无法正常工作。我发现也许方法CGImageSourceCreateThumbnailAtIndex可以帮助我,但是例子只是在Objective C中,对我来说太过分了。

有人可以帮助我如何使用ALAssets创建自定义大小的缩略图?表现出色,表现出色。感谢

1 个答案:

答案 0 :(得分:0)

let objAlAsset = arrayImages.objectAtIndex(0)  as! ALAsset
var image = UIImage(CGImage:objAlAsset.aspectRatioThumbnail().takeUnretainedValue())
let imgData = UIImageJPEGRepresentation(image, 1.0)