在Swift中从ALAsset获取图像

4

我有一些从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
    }

输出只显示了一张图片,我认为是因为代码有问题。
2个回答

9
< p> thumbnail() 函数返回 Unmanaged<CGImage>。如与 Cocoa 数据类型一起工作中所解释的那样,您必须使用takeUnretainedValue() 或者takeRetainedValue()将未受管理的对象转换为内存管理对象。在这种情况下,

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

thumbnail()返回一个未持有对象(它的名称中没有“create”或“copy”)。

Swift 3:

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

(但是,自iOS 9起,资产库框架已被弃用,应改用Photos框架。)

我还有一个疑问。 - Alvin Varghese
嗨@Martin,你解释说函数名不包括“create”或“copy”,因此我们可以推断返回的对象是未保留的。我想知道是否有任何进一步澄清这一点的文档?或者也许有一个函数的例子,我应该使用takeRetainedValue()?谢谢! - ndbroadbent
1
@nathan.f77:这在https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html中有记录。以下是一个使用takeRetainedValue()的示例:https://dev59.com/MoTba4cB1Zd3GeqP86R8#26594980 - Martin R
非常感谢,非常有帮助! - ndbroadbent
po assetSuper.thumbnail().takeUnretainedValue() <CGImage 0x7fd9bb47ec90> - Zeus

0
i dislay image use this way:

let assetslibrary = ZZALAssetsLibrary()
        assetslibrary.asset(for: imgUrl,
        resultBlock: { [weak self](mALAsset) in
            if (mALAsset == nil) {
                return
            }
            //[UIImage imageWithCGImage:resolutionRef scale:1.0f orientation:(UIImageOrientation)representation.orientation];
            if let mCGImageVal = mALAsset?.defaultRepresentation().fullScreenImage().takeUnretainedValue() {
                self?.imageView.image = UIImage(cgImage: mCGImageVal)
            }
        },
        failureBlock: { (mError) in
            print("ZZPhotoBrowserCell Photo from asset library error: ",mError ?? "unkown")
        })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接