win*_*ung 6 ios uicollectionview uicollectionviewcell swift photokit
我正在尝试将用户相册中的所有图像加载到我的应用程序的集合视图中,但在加载其中几个后,应用程序将关闭并返回主菜单.与XCode的连接也断开连接.这不会发生在模拟器中,只会发生在我测试的iPhone 4s上.崩溃前出现的错误消息按发生顺序排列,
我已经找到了我认为导致此问题的代码的几个部分.
var imgFetchResult: PHFetchResult!
override func viewDidLoad() {
super.viewDidLoad()
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
if fetchResult.count > 0
{
println("images found ? \(fetchResult.count)")
self.imgFetchResult = fetchResult
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
println("cellForItemAtIndexPath")
let cell: PhotoThumbnailCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as PhotoThumbnailCollectionViewCell
println("indexpath is \(indexPath.item)")
if( indexPath.item == 0 )
{
cell.backgroundColor = UIColor.redColor() //temp placeholder for camera image
}
else
{
let asset: PHAsset = self.imgFetchResult[indexPath.item] as PHAsset
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info) in cell.setThumbnailImage(result)
})
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
我相信我需要释放记忆但不知道该怎么做.它似乎是正在加载到集合视图的单元格中的图像.
我还发现集合视图不超过4个图像.在第4张图像之后,发生了崩溃.此外,图像未按顺序加载.
问题出现在这行代码中
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info) in cell.setThumbnailImage(result)
})
Run Code Online (Sandbox Code Playgroud)
targetSize传递了参数,其值PHImageManagerMaximumSize是罪魁祸首.我改变它来CGSize(width: 105, height: 105)解决问题.
根据文件 PHImageManagerMaximumSize
使用PHImageManagerMaximumSize选项时,Photos会提供可用于资产的最大图像,而不进行缩放或裁剪.(也就是说,它会忽略resizeMode选项.)
所以,这解释了这个问题.我相信如果它是单个图像,它不应该是一个问题,但如果它是很多图像,设备将耗尽内存.
我希望这有助于其他人.
| 归档时间: |
|
| 查看次数: |
2165 次 |
| 最近记录: |