1 Reply
      Latest reply on Oct 16, 2019 12:45 AM by apal
      tomzach Level 1 Level 1 (0 points)

        I am loading the photos from a users photo album into a collection view similar to how is done in Browsing and Modifying Photo Albums | Apple Developer Documentation . I can not seem to track down why the memory is growing out of control. I use the suggested PHCachingImageManager but all that results are blurry images, freezing scrolling and the memory growing out of control until the application crashes.

         

        In my viewDidLoad I run the code below

         

          PHPhotoLibrary.requestAuthorization { (status: PHAuthorizationStatus) in
          print("photo authorization status: \(status)")
          if status == .authorized && self.fetchResult == nil {
          print("authorized")
        
          let fetchOptions = PHFetchOptions()
          fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
          var tempArr:[PHAsset] = []
          self.fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        
          guard let fetchResult = self.fetchResult else{
          print("Fetch result is empty")
          return
          }
        
          fetchResult.enumerateObjects({asset, index, stop in
          tempArr.append(asset)
          })
        // self.assets = tempArr
        
          self.imageManager.startCachingImages(for: tempArr, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: nil)
        
          tempArr.removeAll()
          print("Asset count after initial fetch: \(self.assets?.count)")
        
          DispatchQueue.main.async {
          // Reload collection view once we've determined our Photos permissions.
          print("inside of main queue reload")
          PHPhotoLibrary.shared().register(self)
          self.collectionView.delegate = self
          self.collectionView.dataSource = self
          self.collectionView.reloadData()
          }
          } else {
          print("photo access denied")
          self.displayPhotoAccessDeniedAlert()
          }
          }

         

        and inside of cellForItemAt: I run the following code

         

        cellForItemAt

         

        guard let fetchResult = self.fetchResult else{
          print("Fetch Result is empty")
          return UICollectionViewCell()
          }
        
          let requestOptions = PHImageRequestOptions()
          requestOptions.isSynchronous = false
          requestOptions.deliveryMode = .highQualityFormat
          //let scale = min(2.0, UIScreen.main.scale)
          let scale = UIScreen.main.scale
          let targetSize = CGSize(width: cell.bounds.width * scale, height: cell.bounds.height * scale)
        
        // let asset = assets[indexPath.item]
          let asset = fetchResult.object(at: indexPath.item)
          let assetIdentifier = asset.localIdentifier
        
          cell.representedAssetIdentifier = assetIdentifier
        
          imageManager.requestImage(for: asset, targetSize: cell.frame.size,
          contentMode: .aspectFill, options: requestOptions) { (image, hashable) in
          if let loadedImage = image, let cellIdentifier = cell.representedAssetIdentifier {
        
          // Verify that the cell still has the same asset identifier,
          // so the image in a reused cell is not overwritten.
          if cellIdentifier == assetIdentifier {
          cell.imageView.image = loadedImage
          }
          }
          }