heitorfr / ios-image-editor

iOS View Controller for image cropping. An alternative to the UIImagePickerController editor with extended features.
MIT License
602 stars 129 forks source link

Fill padding color of cropped image with background color of view #32

Open daisy1754 opened 10 years ago

daisy1754 commented 10 years ago

Currently, if users scale an image smaller than bounds area, a padding area is filled with black color. This PR change the behaviour to filling padding with a color of the background view. The result should be more WYSIWYG for users.

daisy1754 commented 10 years ago

I made another commit to answer discussion above. Please have a look :smile:

adahan commented 9 years ago

Hi, I tried to replace "blackColor" by "clearColor". This doesn't change anything. Someone could give me a hint about how I can get a clear background? Thanks in advance

Edit: I found the answer. My source hadn't any alpha chanel. => Solution to add alpha canal to add in -(CGIImageRef)newTransformedImage method

//ADD ALPHA CHANEL { CGImageAlphaInfo alpha = CGImageGetAlphaInfo(source); BOOL hasAlpha = (alpha == kCGImageAlphaFirst || alpha == kCGImageAlphaLast || alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedLast);

if (!hasAlpha) {
    size_t width = CGImageGetWidth(source);
    size_t height = CGImageGetHeight(source);

    CGContextRef offscreenContext = CGBitmapContextCreate(NULL,
                                                          width,
                                                          height,
                                                          CGImageGetBitsPerComponent(source),
                                                          0,
                                                          CGImageGetColorSpace(source),
                                                          kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), source); 
    source = CGBitmapContextCreateImage(offscreenContext);

    CGContextRelease(offscreenContext);
}

//ADD ALPHA CHANEL