Mathemage
Mathemage

Reputation: 315

draw one image above another code not working

I have 2 images of same size and I need to make 3d picture in which 2nd is above 1st I have UIImage class extension with following method

+(UIImage*)imageFrom2Images:(UIImage *)img1 with:(UIImage *)img2 {

UIGraphicsBeginImageContext(img1.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);      
[img1 drawAtPoint:CGPointMake(0, 0)];
[img2 drawAtPoint:CGPointMake(0, 0)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPopContext();
UIGraphicsEndImageContext();
return resultingImage;

}

I've tried to save resulting image in Photo Album but it calls error and not working..It seems that this method is wrong.

   UIImage*t1=[self.mw.imageFIFO lastObject];
   UIImage* test=[UIImage imageFrom2Images:self.imageView.image with:t1];
   UIImageWriteToSavedPhotosAlbum(test, self,
          @selector(image:didFinishSavingWithError:contextInfo:), nil);

The error message I am getting is:

 Error Domain=ALAssetsLibraryErrorDomain Code=-3304 "Failed to encode image for saved photos." 
 UserInfo=0x9398960 {NSUnderlyingError=0x935e760 "Failed to encode image for saved photos.", 
 NSLocalizedDescription=Failed to encode image for saved photos.}

Upvotes: 1

Views: 401

Answers (2)

Alexey
Alexey

Reputation: 7247

This method should do it:

-(UIImage *)drawFirstImage:(UIImage*)firstImage afterSecondImage:(UIImage *)secondImage
{

    float finalWidth=MAX(firstImage.size.width,secondImage.size.width);
    float finalHeight=firstImage.size.height +  secondImage.size.height;

    CGSize finalSize=CGSizeMake(finalWidth, finalHeight);
    UIGraphicsBeginImageContext(finalSize);

    [firstImage drawInRect:CGRectMake(0, 0, firstImage.size.width, firstImage.size.height)];
    [secondImage drawInRect:CGRectMake(0, firstImage.size.height, secondImage.size.width, secondImage.size.height)];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return resultImage;
}

You can use it as:

 UIImage *resultImage= [self drawFirstImage:image1 afterSecondImage:image2];

Here it is a category implementation:

UIImage+MyExtensions.h

#import <UIKit/UIKit.h>

@interface UIImage (MyExtensions)
-(UIImage *)attachImageBelow:(UIImage *)secondImage;
@end

UIImage+MyExtensions.m

#import "UIImage+MyExtensions.h"

@implementation UIImage (MyExtensions)

-(UIImage *)attachImageBelow:(UIImage *)secondImage{
    float finalWidth=MAX(self.size.width,secondImage.size.width);
    float finalHeight=self.size.height +  secondImage.size.height;

    CGSize finalSize=CGSizeMake(finalWidth, finalHeight);
    UIGraphicsBeginImageContext(finalSize);

    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    [secondImage drawInRect:CGRectMake(0, self.size.height, secondImage.size.width, secondImage.size.height)];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
}

You can use it like this:

#import "UIImage+MyExtensions.h"
UIImage *resultImage= [image1 attachImageBelow:image2];

Upvotes: 0

Cowirrie
Cowirrie

Reputation: 7226

You should never need to get the current graphics context and then push it - you're just duplicating the top of the context stack. It would be nice if it still worked, but it doesn't. Remove the calls to UIGraphicsPushContext and UIGraphicsPopContext, and it works as intended.

+(UIImage*)imageFrom2Images:(UIImage *)img1 with:(UIImage *)img2 {
    UIGraphicsBeginImageContext(img1.size);
    [img1 drawAtPoint:CGPointMake(0, 0)];
    [img2 drawAtPoint:CGPointMake(0, 0)];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

To detect these problems in future, check the output from imageFrom2Images. As originally implemented, it returned nil, so it's not surprising that the following calls didn't know what to do. (If it had returned an image object, the next step would be to display it inside a UIImageView, to make sure it's the correct image.)

Upvotes: 1

Related Questions