我正在尝试将用户相册中的所有图像加载到我的应用程序的收藏视图中,但在加载了其中一些后,应用程序将自行关闭并返回主菜单。与 XCode 的连接也断开了。这不会发生在模拟器中,而只会发生在我正在测试的 iPhone 4s 中。在它崩溃之前出现的错误消息是,按照发生的顺序,
- 收到内存警告
- 与 assetsd 的连接中断或 assetsd 死机。
我已经收集了我认为导致此问题的代码的几个部分。
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
}
我相信我需要释放内存,但不确定要释放什么。似乎是正在加载到集合视图的单元格中的图像。
我还发现集合视图不超过 4 个图像。在第四张图片之后,发生了崩溃。此外,图像未按顺序加载。