CollectionView两次返回相同的图像

时间:2017-01-22 16:26:36

标签: swift uicollectionview swift3

当我请求图片并将它们加载到我的收藏夹View时,请求会同时返回低分辨率图像和高分辨率图像。

enter image description here

正如您所看到的那样,在我的collectionView中,相同的图像以低和高分辨率返回两次。

代码:

var imageArray = [UIImage]()
    let imgManager = PHImageManager.default()

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.delegate = self
    collectionView.dataSource = self
    grabPhoto()
}


func grabPhoto(){

    let imageSize = CGSize(width: 800, height: 800)

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

    if let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) as? PHFetchResult {
        if fetchResult.count > 0 {
            for i in 0..<fetchResult.count {
                imgManager.requestImage(for: fetchResult.object(at: i), targetSize: imageSize, contentMode: .aspectFill , options: nil, resultHandler: { (image, error) in
                    self.imageArray.append(image!)
                    self.photoImageView.image = self.imageArray.first
                    self.collectionView.reloadData()
                     print("Result Size Is \(image?.size)")
                })
            }
         }
       } else {
            print("you got no Photos!")


     }
 }

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageArray.count
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UploadPhotoCell", for: indexPath)
    let imageView = cell.viewWithTag(1) as! UIImageView
    imageView.image = imageArray[indexPath.row]
    return cell
}

0 个答案:

没有答案