UICollection由于添加太多图片时的内存问题而终止

时间:2018-08-30 13:01:14

标签: swift3 uicollectionview

我创建了一个UICollection,用于显示“定制相册”中包含的一组图像。 该应用程序提供了从相机拍摄新照片的功能。 应用程序运行正常,但如果添加过多图片,则会遇到以下错误消息:

  

来自调试器的消息:由于内存问题而终止

这是我的CollectionView中使用的代码

class GallerieViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CLLocationManagerDelegate {

...


@IBAction func btnCamera(_ sender: Any) {


    if(UIImagePickerController.isSourceTypeAvailable(.camera)){
        //load the camera interface
        var picker : UIImagePickerController = UIImagePickerController()
        picker.sourceType = UIImagePickerControllerSourceType.camera
        picker.delegate = self
        picker.allowsEditing = false
        self.present(picker, animated: true, completion: nil)

    }else{
        //no camera available
        var alert = UIAlertController(title: "Erreur", message: "Aucune Caméra disponible", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction!) in
            alert.dismiss(animated: true, completion: nil)
        }))
        self.present(alert, animated: true, completion: nil)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
....

}


//when a cell is selected
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
            _selectedCells.add(indexPath)
            collectionView.reloadItems(at: [indexPath])
        let viewVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewPhotoGallerie") as! ViewPhotoGallerie
        viewVC.index = indexPath.item
        viewVC.photosAsset = self.photosAsset
        viewVC.assetCollection = self.assetCollection
        self.navigationController?.pushViewController(viewVC, animated: true)
    }
}


//during startup of the view
override func viewWillAppear(_ animated: Bool) {
    //fecth the photos from the collection
    self.navigationController?.hidesBarsOnTap = false
    if(self.assetCollection != nil){
        self.photosAsset = PHAsset.fetchAssets(in: self.assetCollection, options: nil)
        let photoCount = self.photosAsset.count

        if photoCount == 0 {
            //self.navigationItem.title = "No Photos"

        } else {
            //self.navigationItem.title = "\(photoCount) Photos"
        }
    }

    //Handle no photos in the assetCollection
    //...Have a label saying, no photos
    self.collectionView.reloadData()
}



public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
    let cell: GalleryCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! GalleryCollectionViewCell

        //Modify the cell
        let asset: PHAsset = self.photosAsset[indexPath.item] as PHAsset
        let option = PHImageRequestOptions()
        option.isNetworkAccessAllowed = true
        option.isSynchronous = true
        PHImageManager.default().requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: option, resultHandler: {(result:UIImage?, nil)in
            cell.setThumbnailImage(thumbnailImage: result!)
        })
    return cell
}

//UIImagePickerControllerDelegate Methods
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){

    if let image: UIImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {

            // Create asset for the taken image and save it
            PHPhotoLibrary.shared().performChanges({
                let assetToCreate = PHAssetChangeRequest.creationRequestForAsset(from: image)

                if let assetPlaceholder = assetToCreate.placeholderForCreatedAsset {
                    self.photoIdentifier = assetPlaceholder.localIdentifier // Retrive the local identifier of the photo

                     if let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection, assets: self.photosAsset) {
                        albumChangeRequest.addAssets(([assetPlaceholder] as NSArray?)!)
                                //save Photo Object in memory
                                let encodedData = NSKeyedArchiver.archivedData(withRootObject: PhotoGallery.sharedInstance.photoGallery)


                     }
                }
            },
            completionHandler: {(success, error) -> Void in
                NSLog("Adding Image to library > %@", (success ? "Success":"Error"))
                DispatchQueue.main.async {
                    picker.dismiss(animated: true, completion: nil)
                }

            })

    }

}

public func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
    picker.dismiss(animated: true, completion: nil)
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
    let availableWidth = view.frame.width - paddingSpace
    let widthPerItem = availableWidth / itemsPerRow

    return CGSize(width: widthPerItem, height: widthPerItem)
}
}

我不确定为什么会出现这种错误,可能是我必须减小CollectionView单元格上显示的缩略图的大小,还是可能需要在拍摄照片并释放相机后释放内存?

0 个答案:

没有答案