Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

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

wants to merge 2 commits into from

Conversation

daisy1754
Copy link

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
Copy link
Author

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

@adahan
Copy link

adahan commented Jan 20, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants