IGRSoft / IGRPhotoTweaks

Drag, Rotate, Scale and Crop
https://igrsoft.com
MIT License
237 stars 71 forks source link

Crash while cropping panorama image #16

Closed GaneshManickam closed 6 years ago

GaneshManickam commented 7 years ago

App getting crashed while cropping panorama image

porzal commented 7 years ago

Not sure if this is the same crash @GaneshManickam had. The crash happens at the following line in CGImage+IGRPhonoTweakExtension.swift: let result = context!.makeImage()!

Oct 26 23:00:10 ...: CGBitmapContextCreate: unsupported parameter combination: set CGBITMAP_CONTEXT_LOG_ERRORS environmental variable to see the details fatal error: unexpectedly found nil while unwrapping an Optional value

To recreate the crash, take a snapshot on your iPhone or simulator. The crash does not happen if it is a regular image.

So I set CGBITMAP_CONTEXT_LOG_ERRORS = 1 in env variable to get the details. I hope it helps: Oct 26 23:12:02 ...: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; RGB color space model; kCGImageAlphaLast; 4992 bytes/row. Valid parameters for RGB color space model are: 16 bits per pixel, 5 bits per component, kCGImageAlphaNoneSkipFirst 32 bits per pixel, 8 bits per component, kCGImageAlphaNoneSkipFirst 32 bits per pixel, 8 bits per component, kCGImageAlphaNoneSkipLast 32 bits per pixel, 8 bits per component, kCGImageAlphaPremultipliedFirst 32 bits per pixel, 8 bits per component, kCGImageAlphaPremultipliedLast 64 bits per pixel, 16 bits per component, kCGImageAlphaPremultipliedLast 64 bits per pixel, 16 bits per component, kCGImageAlphaNoneSkipLast 64 bits per pixel, 16 bits per component, kCGImageAlphaPremultipliedLast|kCGBitmapFloatComponents 64 bits per pixel, 16 bits per component, kCGImageAlphaNoneSkipLast|kCGBitmapFloatComponents 128 bits per pixel, 32 bits per component, kCGImageAlphaPremultipliedLast|kCGBitmapFloatComponents 128 bits per pixel, 32 bits per component, kCGImageAlphaNoneSkipLast|kCGBitmapFloatComponents See Quartz 2D Programming Guide (available online) for more information. fatal error: unexpectedly found nil while unwrapping an Optional value

denismars commented 6 years ago

Same here - seems this library is becoming stale. Hope we can fix.

GaneshManickam commented 6 years ago

I'm using below code to avoid crash but still this library not support panorama image crop. Use below code to avoid crash

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
        print("didFinishPickingMediaWithInfo")
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            picker.dismiss(animated: false, completion: { () -> Void in

                let smallest = min(pickedImage.size.width, pickedImage.size.height)
                let largest = max(pickedImage.size.width, pickedImage.size.height)
                let ratio = largest/smallest
                if (ratio >= CGFloat(2/1)) || (ratio >= CGFloat(4/1)) || (ratio >= CGFloat(10/1)) {
                    // it is probably a panorama
                    print( "Panorama can't set for profile image")
                } else {
                    //Pass image to cropper here
                }
            })
        }
    }
ykying commented 6 years ago

Because library using the original image width as the output image width. E.x: 100000x500 -> 1:1 crop -> 100000x100000 (memory issue crash) Simple fix: CGImage+IGRPhonoTweakExtension.swift New param: zoomScale func transformedImage(_ transform: CGAffineTransform, zoomScale: CGFloat, sourceSize: CGSize, cropSize: CGSize, imageViewSize: CGSize) -> CGImage { let expectedWidth = floor(sourceSize.width / imageViewSize.width * cropSize.width) / zoomScale let expectedHeight = floor(sourceSize.height / imageViewSize.height * cropSize.height) / zoomScale let outputSize = CGSize(width: expectedWidth, height: expectedHeight) let bitmapBytesPerRow = 0

IGRPhotoTweakViewController.swift Line 126, replace the function call let imageRef = fixedImage.transformedImage(transform, zoomScale: self.photoView.scrollView.zoomScale, sourceSize: self.image.size, cropSize: self.photoView.cropView.frame.size, imageViewSize: self.photoView.photoContentView.bounds.size)

ikorich commented 6 years ago

@ykying Thank You!

Merged fix