У меня есть изображения, которые возвращаются с камеры ALAssetsLibrary. Теперь я хочу отобразить его в файле UIImageView
. Я попробовал этот код, но он показывает некоторые ошибки:image from ALAsset in 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 раз, я думаю. В чем проблема с этим кодом?
У меня есть еще один сомнение –
Привет @Martin, вы объяснили, что имя функции не включает в себя «создать» или «скопировать» на свое имя, поэтому мы можем сделать вывод о том, что возвращаемый объект отсутствует. Мне было интересно, можете ли вы указать мне на любую документацию, которая разъясняет это дальше? Или, может быть, пример функции, где я должен использовать 'takeRetainedValue()' вместо этого? Благодаря! – ndbroadbent
@ nathan.f77: Он задокументирован в https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html. Вот пример, где вы бы использовали takeRetainedValue(): http://stackoverflow.com/a/26594980/1187415 –