gpt4 book ai didi

ios - swift 从 ALAsset 获取图像

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:07 25 4
gpt4 key购买 nike

我有 ALAssetsLibrary 从相机返回的图像。现在,我想在 UIImageView 中显示它。我试过这段代码,但它显示了一些错误:

var assets : ALAsset = photoArray.objectAtIndex(indexPath.row) as ALAsset

cell.cameraOutlet.image = UIImage(CGImage: assets.thumbnail()) as UIImage // Showing error - Missing argument for parameter 'inBundle' in call

在那之后,我尝试了这段代码:

var assets : ALAsset = (photoArray.objectAtIndex(indexPath.row) as ALAsset)
var imageRef : CGImageRef = assets.thumbnail() as CGImageRef // showing error - Unmanaged<CGImage>, is not convertible to CGImageRef
cell.cameraOutlet.image = UIImage(CGImage: assets.thumbnail()) as UIImage // Showing error - Missing argument for parameter 'inBundle' in call

我想显示图片,我该怎么做?

dispatch_async(dispatch_get_main_queue(), {
self.photoLibrary.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAlbum),
usingBlock: {

(group: ALAssetsGroup!, stop: UnsafePointer<ObjCBool>) -> Void in
if group != nil {
group.enumerateAssetsUsingBlock({
(asset: ALAsset!, index: Int, stop: UnsafePointer<ObjCBool>) -> Void in

if asset != nil
{
if asset.valueForProperty(ALAssetPropertyType).isEqualToString(ALAssetTypePhoto)
{
self.photoArray.addObject(asset)
}
}
})
}
self.collectionView.reloadData()
},
failureBlock: {
(myerror: NSError!) -> Void in
println("error occurred: \(myerror.localizedDescription)")
})
})

override func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return photoArray.count
}


override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return 1
}

override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
let cell : CameraCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Camera", forIndexPath: indexPath) as CameraCollectionViewCell

var asset : ALAsset = photoArray.objectAtIndex(indexPath.row) as ALAsset
cell.cameraOutlet.image = UIImage(CGImage: asset.thumbnail().takeUnretainedValue()) as UIImage
return cell
}

我认为输出仅显示一张图像 100 次。这段代码有什么问题?

最佳答案

thumbnail()函数返回 Unmanaged<CGImage> .如中所述 Working with Cocoa Data Types ,您必须使用 takeUnretainedValue() 将非托管对象转换为内存托管对象或 takeRetainedValue() .在这种情况下

cell.cameraOutlet.image = UIImage(CGImage: asset.thumbnail().takeUnretainedValue())

因为thumbnail()返回一个未保留的对象(没有“创建”或“复制”以其名称)。

swift 3:

cell.cameraOutlet.image = UIImage(cgImage: asset.thumbnail().takeUnretainedValue())

(但是,从 iOS 9 开始,资源库框架已弃用应该改用照片框架。)

关于ios - swift 从 ALAsset 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24970128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com