0

I need to obtain a new image from 2 existing images. (the second one should be shown as a logo under the first one). The problem is if the top (nevr tried with the bottom one) has a transparent background it show up as black. This is the code:

+(UIImage *) addBottomOverlayWithImage:(UIImage*) fgImage
                             inImage:(UIImage*) bgImage {

    if(!bgImage) {
        return nil;
    }

    CGFloat minSize = MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    CGSize maxImageSize = CGSizeMake(minSize-140, minSize-140);
    CGSize imageSize = CGSizeMake(maxImageSize.width,
                                  maxImageSize.height+fgImage.size.height*2);// image size including art work and logo

    UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [bgImage drawInRect:CGRectMake( 0, 0, maxImageSize.width, maxImageSize.height)];

    [[UIColor whiteColor] setFill];

    CGRect targetForFgImage = CGRectMake(0, imageSize.height-fgImage.size.height, imageSize.width, fgImage.size.height); // bottom part , where the logo will be added
    CGContextFillRect(context, targetForFgImage);

    CGFloat widthForFgImage = MIN(imageSize.width - 22.0, fgImage.size.width);
    CGFloat heightForFgImage = MIN(fgImage.size.height,widthForFgImage * 0.3);

    UIImage *imageToUse = [fgImage imageScaledToFitSize:CGSizeMake(widthForFgImage, heightForFgImage)]; // the logo size

    CGFloat xVal = 11.0;
    if (imageSize.width > widthForFgImage) {
        xVal = (imageSize.width -  widthForFgImage) / 2.0;
    }

    CGFloat yVal;
    if (targetForFgImage.size.height > heightForFgImage) {
        CGFloat diff = targetForFgImage.size.height - heightForFgImage;
        yVal = (imageSize.height - fgImage.size.height + (diff / 2.0));
    } else {
        yVal = imageSize.height - heightForFgImage;
    }

    [imageToUse drawInRect:CGRectMake(xVal, yVal, widthForFgImage, heightForFgImage)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

How should I fix this issue?

user426132
  • 1,341
  • 4
  • 13
  • 28

1 Answers1

1

Just make

UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height)), NO, 1);

Set it NO for more info check developer document of apple

9to5ios
  • 5,319
  • 2
  • 37
  • 65