Alvin Varghese
Alvin Varghese

Reputation: 822

image from ALAsset in swift

I have images that returned from the camera by the ALAssetsLibrary. Now, I want to display it in a UIImageView. I tried this code, but it shows some errors:

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 

after that, I tried this code:

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 

I want to display the image, how can I do this?

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
    }

The output shows only one image for a 100 time I think. Whats the problem with this code?

Upvotes: 4

Views: 4987

Answers (2)

buoge
buoge

Reputation: 459

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")
        })

Upvotes: 0

Martin R
Martin R

Reputation: 540075

The thumbnail() function returns Unmanaged<CGImage>. As explained in Working with Cocoa Data Types, you have to convert the unmanaged object to a memory managed object with takeUnretainedValue() or takeRetainedValue(). In this case

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

because thumbnail() returns an unretained object (is does not have "create" or "copy" in its name).

Swift 3:

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

(However, the Asset Library framework is deprecated as of iOS 9 and the Photos framework should be used instead.)

Upvotes: 9

Related Questions