我正在寻找解决方案如何显示照片库中保存的图像的缩略图。我知道有缩略图的方法,但结果太小(和对面的全屏尺寸太大)。我想设置我想要的缩略图的自定义大小。我试图寻找任何解决方案,但我觉得太复杂了。目前,我有以下显示拇指的方法:
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
方法可以帮助我,但示例只在目标c中,对我来说太多了,无法使其工作。那么,有人能帮我如何从alasset创建自定义大小的缩略图吗?表现出色。谢谢
最佳答案
let objAlAsset = arrayImages.objectAtIndex(0) as! ALAsset
var image = UIImage(CGImage:objAlAsset.aspectRatioThumbnail().takeUnretainedValue())
let imgData = UIImageJPEGRepresentation(image, 1.0)
关于swift - Swift调整照片库中的图像(ALAssets)的缩放比例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29040379/