Tim
Tim

Reputation: 13

Merge and save two UIImageViews

My project has two UIImages, one named imageView and one named Birdie.

- (UIImage *)addImage:(UIImage *)imageView toImage:(UIImage *)Birdie {
    UIGraphicsBeginImageContext(imageView.size);

    // Draw image1
    [imageView drawInRect:CGRectMake(0, 0, imageView.size.width, imageView.size.height)];

    // Draw image2
    [Birdie drawInRect:CGRectMake(0, 0, Birdie.size.width, Birdie.size.height)];

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return resultingImage;
}

-(void)captureScreen{
    UIImageWriteToSavedPhotosAlbum(resultingImage, nil, nil, nil);
}

This is my code, the captureScreen button is to save the image. It's not working because resultingImage is undeclared, but I'm pretty new to Xcode so I don't know how to fix this.

Many thanks in advance!

Upvotes: 0

Views: 2350

Answers (2)

Garnik
Garnik

Reputation: 423

You can do something like this.

Override this -(void)captureScreen: method to -(void)captureScreen:(UIImage*)imageToSave

then in your button action like

- (IBAction)myButton:(id)sender{ [self captureScreen:[self addImage:imageViewImage toImage:BirdieImage]] }

Upvotes: 1

Ravin
Ravin

Reputation: 8564

  `resultingImage` 

Is not a global variable its scope is limited to only - (UIImage *)addImage:(UIImage *)imageView toImage:(UIImage *)Birdie method you have to call this method and use returned object by this method.

-(void)captureScreen{
     UIImage *resultingImage = [self addImage:yourFirstImage toImage:yourSecondImage];
    UIImageWriteToSavedPhotosAlbum(resultingImage, nil, nil, nil);
}

Tim, one point you should remember tht we must not call 'drawRect' method directly. It would be called by the system. So here you should use other methods available for UIImage. you can use 'imageNamed:' method....

Upvotes: 0

Related Questions