我有通过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次的图像。这个代码有什么问题?
我有一个更疑问 –
Hi @Martin,你解释过函数名不包含名称中的“create”或“copy”,所以我们可以推断返回的对象是不存在的。我想知道你是否可以将我指向任何可以进一步阐明这一点的文档?或者,也许我应该使用'takeRetainedValue()'而不是函数的例子?谢谢! – ndbroadbent
@ nathan.f77:记载于https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html。下面是一个使用takeRetainedValue()的例子:http://stackoverflow.com/a/26594980/1187415 –