swift UIGraphicsGetImageFromCurrentImageContext can't release memory

前端 未结 2 997
清歌不尽
清歌不尽 2021-02-05 08:39

Swift Code

When we get a screenshot of a UIView, we use this code usually:

UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
drawViewHie         


        
相关标签:
2条回答
  • 2021-02-05 08:42

    I solved this problem by putting image operations in another queue!

    private func processImage(image: UIImage, size: CGSize, completion: (image: UIImage) -> Void) {
        dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0)) {
            UIGraphicsBeginImageContextWithOptions(size, true, 0)
            image.drawInRect(CGRect(origin: CGPoint.zero, size: size))
            let tempImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            completion(image: tempImage)
        }
    }
    
    0 讨论(0)
  • 2021-02-05 08:55
    private extension UIImage
    {
        func resized() -> UIImage {
            let height: CGFloat = 800.0
            let ratio = self.size.width / self.size.height
            let width = height * ratio
    
            let newSize = CGSize(width: width, height: height)
            let newRectangle = CGRect(x: 0, y: 0, width: width, height: height)
    
            UIGraphicsBeginImageContext(newSize)
            self.draw(in: newRectangle)
    
            let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return resizedImage!
        } 
    }
    
    0 讨论(0)
提交回复
热议问题