来自快速的ALAsset的图像

Alv*_*ese 4 ios swift

我有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 
Run Code Online (Sandbox Code Playgroud)

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

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 
Run Code Online (Sandbox Code Playgroud)

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

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
    }
Run Code Online (Sandbox Code Playgroud)

我认为输出只显示了100张图像.这个代码有什么问题?

Mar*_*n R 9

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

cell.cameraOutlet.image = UIImage(CGImage: asset.thumbnail().takeUnretainedValue())
Run Code Online (Sandbox Code Playgroud)

因为thumbnail()返回一个未保留的对象(名称中没有"创建"或"复制").

斯威夫特3:

cell.cameraOutlet.image = UIImage(cgImage: asset.thumbnail().takeUnretainedValue())
Run Code Online (Sandbox Code Playgroud)

(但是,从iOS 9开始,不推荐使用资源库框架,而应使用Photos框架.)