1

整体内存增长

记忆增长

每当我收到内存警告时,我都会清除缓存

func applicationDidReceiveMemoryWarning(_ application: UIApplication) {

        print("Memory warning...")
        imageCache.countLimit = 0
        imageCache.totalCostLimit = 0
        imageCache.removeAllObjects()
        SDImageCache.shared.clearMemory()
        URLCache.shared.removeAllCachedResponses()
    }

这是下载图像和视频缩略图的代码

     func collectionView(_ collectionView: UICollectionView, 
          cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

      image_url = [InputSource]()
     if item.media_url?.contains(".mp4") ?? false
     {

         DispatchQueue.global().async {

             UIImage.getThumbnailImage(forUrl: url) { (image) in
                             DispatchQueue.main.async {

                             self.imageView_Video.image = image
                        }
                    }
                }
            }
            else{

                image_url.append(AlamofireSource(url: url , placeholder: UIImage(named: AppConstants.image_placeholder)))
            }
            }
  1. 要下载图像,我正在使用图像幻灯片 3rd 方库
  2. 要下载缩略图,我使用以下代码,

     static func getThumbnailImage(forUrl url: URL, completion: @escaping(_ 
      image: UIImage) -> Void) {
    
          DispatchQueue.global().async {
    
            do{
            let asset = AVURLAsset(url: url, options: nil)
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
    
            DispatchQueue.main.async {
                let image = UIImage(cgImage: cgImage)
                completion(image)
            }
    
        } catch let error{
            print("*** Error generating thumbnail: \(error.localizedDescription)")
    
            DispatchQueue.main.async {
                completion(UIImage())
            }
        }
    }
    }
    

    如果我评论以下代码,应用程序不会崩溃

            image_url.append(AlamofireSource(url: url , placeholder: 
                UIImage(named: AppConstants.image_placeholder)))
    
4

1 回答 1

0

我通过更改下载图像方法解决了这个问题,如下所示,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{
   if let medias = history?.medias
    {
        if medias.count > 0
        {
            slides = [];

            for item in medias{

                if let url = URL(string: item.media_url ?? " "){

                    if item.media_url?.contains(".mp4") ?? false
                    {

                        self.imageView_Video.backgroundColor = UIColor.black.withAlphaComponent(0.8)
                        DispatchQueue.global().async {

                            if let cachedImage = AppDelegate.shared.imageCache.object(forKey: "\(url)" as NSString) {
                                DispatchQueue.main.async {
                                    self.imageView_Video.image = cachedImage
                                }
                            }else{
                                UIImage.getThumbnailFromUrl("\(url)") { [weak self] (image) in
                                    DispatchQueue.main.async {
                                        if let img = image{
                                            AppDelegate.shared.imageCache.setObject(img, forKey: "\(url)" as NSString? ?? NSString())
                                            self?.imageView_Video.image = img
                                        }
                                    }
                                }
                            }

                        }
                    }
                    else{


                        let slide1:Slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide

                        let processor = DownsamplingImageProcessor(size: CGSize(width: 500, height: 500))
                            |> RoundCornerImageProcessor(cornerRadius: 0)
                        slide1.imageView?.kf.setImage(
                            with: url,
                            placeholder: UIImage(named: "i_placeholder"),
                            options: [
                                .processor(processor),
                            ])
                        { [weak self]
                            result in
                            switch result {
                            case .success(let value):

                                self?.slides.append(slide1)

                            case .failure(let error):

                                slide1.imageView?.image = UIImage(named: "i_placeholder")
                                self?.slides.append(slide1)

                            }

                            self?.setupSlideScrollView(slides: self?.slides ?? [])
                        }

                    }//end else
                }//end if let url

            }
        }
    }
 }

我删除了幻灯片库并添加了翠鸟以下载高分辨率图像。

于 2019-12-24T12:30:06.670 回答