Ios 来自阿拉斯加州斯威夫特的图像
我有图书馆从相机返回的图像。现在,我想在Ios 来自阿拉斯加州斯威夫特的图像,ios,swift,Ios,Swift,我有图书馆从相机返回的图像。现在,我想在UIImageView中显示它。我尝试了此代码,但它显示了一些错误: var assets : ALAsset = photoArray.objectAtIndex(indexPath.row) as ALAsset cell.cameraOutlet.image = UIImage(CGImage: assets.thumbnail()) as UIImage // Showing error - Missing argument for parame
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
之后,我尝试了以下代码:
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
var资产:ALAsset=(photoArray.objectAtIndex(indexPath.row)作为ALAsset)
var imageRef:CGImageRef=assets.thumbnail()作为CGImageRef//显示错误-未管理,无法转换为CGImageRef
cell.cameraOutlet.image=UIImage(CGImage:assets.thumbnail())作为UIImage//显示错误-调用中缺少参数“inBundle”的参数
我想显示图像,我该怎么做
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
}
dispatch\u async(dispatch\u get\u main\u queue(){
self.photoLibrary.enumerateGroupsWithTypes(AlasSetGroupType(AlasSetGroupAlbum)),
使用块:{
(组:ALASSETGROUP!,停止:UnsafePointer)->在中无效
如果组!=nil{
group.AssetSSingBlock({
(asset:ALAsset!,索引:Int,stop:UnsafePointer)->Void in
如果资产!=零
{
如果asset.valueForProperty(ALAssetPropertyType).IseQualString(ALAssetTypePhoto)
{
self.photoArray.addObject(资产)
}
}
})
}
self.collectionView.reloadData()
},
故障块:{
(myerror:NSError!)->在中无效
println(“发生错误:\(myerror.localizedDescription)”)
})
})
重写func numberOfSectionsInCollectionView(collectionView:UICollectionView!)->Int{
//#警告:方法实现不完整--返回节数
返回photoArray.count
}
重写func collectionView(collectionView:UICollectionView!,numberOfItemsInSection:Int)->Int{
//#警告:方法实现不完整--返回节中的项数
返回1
}
重写func collectionView(collectionView:UICollectionView!,cellForItemAtIndexPath indexPath:NSIndexPath!)->UICollectionViewCell!{
将cell:CameraCollectionViewCell=collectionView.dequeueReusableCellWithReuseIdentifier(“Camera”,forIndexPath:indexPath)作为CameraCollectionViewCell
var资产:ALAsset=photoArray.objectAtIndex(indexPath.row)作为ALAsset
cell.cameraOutlet.image=UIImage(CGImage:asset.thumbnail().TakeUnrepainedValue())作为UIImage
返回单元
}
我认为输出100次只显示一个图像。此代码有什么问题?函数返回非托管。如中所述
,必须使用takeUnrepainedValue()
或takeRetainedValue()
将非托管对象转换为内存托管对象。
在这种情况下
cell.cameraOutlet.image = UIImage(CGImage: asset.thumbnail().takeUnretainedValue())
因为thumbnail()
返回一个未保存的对象(is没有“创建”或“复制”
以其名义)
Swift 3:
cell.cameraOutlet.image = UIImage(cgImage: asset.thumbnail().takeUnretainedValue())
(但是,从iOS 9开始,资产库框架已被弃用。)
应该使用照片框架。)我还有一个疑问hi@Martin,您解释说函数名中不包含“create”或“copy”,因此我们可以推断返回的对象是未恢复的。我想知道你是否能给我指出任何进一步澄清这一点的文档?或者是一个我应该使用takeRetainedValue()
的函数示例?谢谢@nathan.f77:记录在中。下面是一个使用takeRetainedValue()的示例:非常感谢,这非常有用!采购订单AssetSusper.thumbnail().TakeUnrepainedValue()
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")
})