0

我正在像这样旋转 UIImageView:

imageView.transform = imageView.transform.concatenating(CGAffineTransform(rotationAngle: CGFloat(radians)))

用户通过单击确认按钮确认旋转后,以弧度为单位的旋转角度计算如下:

var rotationAngleRadians: Double {
    Double(imageView.transform.rotationAngleRadians)
}

extension CGAffineTransform {
    public var rotationAngleRadians: CGFloat {
        return atan2(b, a)
    }
}

最后使用此方法(Source)旋转 UIImage :

func rotated(radians: Float) -> UIImage? {
    var newSize = CGRect(origin: CGPoint.zero, size: self.size).applying(CGAffineTransform(rotationAngle: CGFloat(radians))).size
    // Trim off the extremely small float value to prevent core graphics from rounding it up
    newSize.width = floor(newSize.width)
    newSize.height = floor(newSize.height)
    
    let renderer = UIGraphicsImageRenderer(size: newSize)
    
    let newImage = renderer.image { (context) in
        context.cgContext.translateBy(x: newSize.width / 2, y: newSize.height / 2)
        // Rotate around middle
        context.cgContext.rotate(by: CGFloat(radians))
        draw(in: CGRect(x: -size.width / 2, y: -size.height / 2, width: size.width, height: size.height))
    }
    
    return newImage
}

我在我的视图模型上使用它,如下所示:

func rotated(image: UIImage,
             rotationAngleRadians: Double,
             completion: @escaping (UIImage?) -> Void) {
    DispatchQueue.global(qos: .userInitiated).async {[weak self] in
        func completed(image: UIImage?) {
            DispatchQueue.main.async {
                completion(image)
            }
        }
        if let img = image.rotated(radians: Float(rotationAngleRadians)),
           img.size != .zero { //TODO: It must be fixed. For preventing saving crash on disk is used this approach.
            self?.editPage(editedImage: img, rotationAngleRadians: rotationAngleRadians)
            completed(image: img)
        } else {
            completed(image: nil)
        }
    }
}

但有时旋转的图像会返回零大小的图像:

<UIImage:0x283986520 anonymous {0, 0}>

回溯:

[Unknown process name] CGBitmapContextInfoCreate: unable to allocate 1393990272 bytes for bitmap data
[Unknown process name] CGDisplayListDrawInContext: invalid context 0x0.
Backtrace:
<<redacted>+1796>
<<redacted>+56>
<<redacted>+40>
<<redacted>+40>
  <<redacted>+468>
   <<redacted>+124>
    <<redacted>+196>
     <$sSo7UIImageC10E7rotated7radiansABSgSf_tF+1760>
      <$s1010PERotateVMC7rotated5image20rotationAngleRadians10completionySo7UIImageC_SdyAISgctFyycfU_+100>
       <$sIeg_IeyB_TR+52>
        <_dispatch_call_block_and_release+32>
         <_dispatch_client_callout+20>
          <_dispatch_root_queue_drain+824>
           <_dispatch_worker_thread2+140>
            <_pthread_wqthread+216>                 <start_wqthread+8>
[Unknown process name] CGBitmapContextCreateImage: invalid context 0x0.
Backtrace:
<CGBitmapContextCreateImage+336>
 <<redacted>+64>
  <<redacted>+40>
   <<redacted>+40>
    <<redacted>+468>
     <<redacted>+124>
      <<redacted>+196>
       <$sSo7UIImageC10E7rotated7radiansABSgSf_tF+1760>
      <$s1010PERotateVMC7rotated5image20rotationAngleRadians10completionySo7UIImageC_SdyAISgctFyycfU_+100>
       <$sIeg_IeyB_TR+52>
        <_dispatch_call_block_and_release+32>
         <_dispatch_client_callout+20>
          <_dispatch_root_queue_drain+824>
           <_dispatch_worker_thread2+140>
            <_pthread_wqthread+216>                      <start_wqthread+8>

我怎样才能解决这个问题?

4

0 回答 0