2014-07-26 49 views
4

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

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次的图像。这个代码有什么问题?

回答

9

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

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

因为thumbnail()返回一个不保留对象(是没有“创造”或在其名称中的“复制” )。

夫特3:

​​

(但是,在资源库框架弃用的iOS 9的 和照片的框架应改用)

+0

我有一个更疑问 –

+0

Hi @Martin,你解释过函数名不包含名称中的“create”或“copy”,所以我们可以推断返回的对象是不存在的。我想知道你是否可以将我指向任何可以进一步阐明这一点的文档?或者,也许我应该使用'takeRetainedValue()'而不是函数的例子?谢谢! – ndbroadbent

+1

@ nathan.f77:记载于https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html。下面是一个使用takeRetainedValue()的例子:http://stackoverflow.com/a/26594980/1187415 –

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