Проблема с памятью из-за стекирования BackgroundQueue - PullRequest
0 голосов
/ 17 июня 2020

Я делаю приложение для рисования. Когда пользовательское прикосновение закончилось, я беру снимок экрана представления, понижаю разрешение изображения и применяю его к ячейке представления-коллекции. Когда пользователь обращается так быстро, происходит сбой памяти.

  override func drawingViewDrawingEnded(at location: CGPoint) {

        DispatchQueue.global(qos: .background).async {

            super.drawingViewDrawingEnded(at: location)
            if LayerManager.shared.layerDatas[self.drawingView.currentSelectDrawingLayerIndex()].layerType == .drawingLayer{
                let currentLayerImagePath = self.drawingView.getScreenShortForCurrentSelectLayer()
                DispatchQueue.main.async {
                    let url = NSURL(fileURLWithPath: currentLayerImagePath)
                    DispatchQueue.cancelPreviousPerformRequests(withTarget: <#T##Any#>)
                    let image:UIImage = self.downsample(imageAt: url as! URL, to: CGSize(width: 400, height: 400), scale: 1)
                    self.layerToolBar.updateCellImage(image:image,index:LayerManager.shared.currentActiveLayer)
                }
            }
        }

        isPresentSettingView = false
        self.manageToolBarsAtDrawingEnded()
    }

func downsample(imageAt imageURL: URL, to pointSize: CGSize, scale: CGFloat) -> UIImage {
    let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
    let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, imageSourceOptions)!
    let maxDimensionInPixels = max(pointSize.width, pointSize.height) * scale
    let downsampleOptions =
        [kCGImageSourceCreateThumbnailFromImageAlways: true,
         kCGImageSourceShouldCacheImmediately: true,
         kCGImageSourceCreateThumbnailWithTransform: true,
         kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels] as CFDictionary
    let downsampledImage =
        CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions)!
    return UIImage(cgImage: downsampledImage)
}

func updateCellImage(image:UIImage,index:Int){
    LayerManager.shared.layerDatas[index].layeThumbImage = image
    layerCollectionView.reloadItems(at: [IndexPath(item:index, section: 0)])
}

В настоящее время, когда пользователь нажимает так быстро, происходит загрузка фоновой очереди и проблема с памятью. Как избежать такого скопления?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...