2017-05-03 18 views
0

私はこの機能を持っています。この関数は、ギャラリービューのすべてのメディアファイルを初期化します。ギャラリーからアプリケーションに画像を取り込む際のメモリリーク

func initMediaFiles() { 
    var assets: [PHAsset] = [] 
    let options = PHFetchOptions() 
    options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] 

options.fetchLimit = 1000 
    let results = PHAsset.fetchAssets(with: .image, options: options) 
    results.enumerateObjects({ (object, _, _) in 
     if let asset = object as? PHAsset { 
      assets.append(asset) 

     } 
    })   
SCPAsset.imageManager.startCachingImages(for: assets, targetSize: CGSize(width: 110.0, height: 147.0),contentMode: .aspectFill, options: nil) 
    for asset in assets { 
     let scpAsset = SCPAsset(initWithPHAsset: asset) 
     scpAsset.inspectionUUID = self.inspectionId! 
     self.mediaAssets.append(scpAsset) 
    } 

} 

ギャラリービューを読み込むたびに、なぜ10,000のメモリリークが発生するのかわかりません。

答えて

0

ここで私は役立つかもしれないプロジェクトでやった何か:

一部のプロパティ:

let requestOptions = PHImageRequestOptions() 
    let fetchOptions = PHFetchOptions() 
    let cachingImageManager = PHCachingImageManager() 
    var assets: [PHAsset] = [] { 
     willSet { 
      cachingImageManager.stopCachingImagesForAllAssets() 
     } 

     didSet { 
      cachingImageManager.startCachingImagesForAssets(assets, 
       targetSize: PHImageManagerMaximumSize, 
       contentMode: .AspectFit, 
       options: self.requestOptions 
      ) 
     } 
    } 


    func fetchPhotos() { 
     requestOptions.resizeMode = PHImageRequestOptionsResizeMode.Exact 
     requestOptions.version = .Current 
     requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat 
     requestOptions.synchronous = true 

     fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)] 
     if #available(iOS 9.0, *) { 
      fetchOptions.fetchLimit = 50 
     } else { 

      // Fallback on earlier versions 
     } 

     fetchResults = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) 
     guard let fetchResults = fetchResults else { 
      print("No PHAssets") 
      return 
     } 
     dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [weak self] in 
      fetchResults.enumerateObjectsUsingBlock{ object, index, stop in 
       let asset = object as! PHAsset 
       self?.assets.append(asset) 
      } 
      dispatch_async(dispatch_get_main_queue()) { 
       self?.photoCollectionView.reloadData() 
      } 
     } 
    } 
関連する問題