views:

38

answers:

2

Hi all, I want to merge two images where one is a greeting card with a heart shaped portion transparent and another is the image of a couple. Now I want the image of couple movable so that i can move them and resize them to set in that heart part and then I want to save that mixture of image as one image and send it as attachment in mail... Can you please provide me the link of any such examples or solution.

Any help would be really appreciated.

Thanks in advance...

A: 

Maybe something like that:

UIGraphicsBeginImageContext(targetSize);
CGContextRef context = UIGraphicsGetCurrentContext();       
UIGraphicsPushContext(context);                             

[image drawInRect:imageRect];
[topimage drawInRect:topImageRect];

UIGraphicsPopContext();                             

// get a UIImage from the image context
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

// clean up drawing environment
UIGraphicsEndImageContext();

then image is composition of image and topImage and you can set it custom rect, rotate ( using CG) etc. :)

and to send via mail you can do something like this:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@""];
NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
[picker addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"image.jpg"];
NSString *emailBody = @"this is image !");
[picker setSubject:emailBody];
[picker setMessageBody:emailBody isHTML:YES];
[picker release];

Cheers, Krzysztof Zabłocki

Krzysztof Zabłocki
A: 

This is how i have done masking and this worked for me.

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage1
{

    float width,height;
        width=288.0;
        height=274.0;

    CGContextRef mainViewContentContext; CGColorSpaceRef colorSpace;
    UIImage *orgImage=image; 

    CGImageRef img = [orgImage CGImage];

    colorSpace = CGColorSpaceCreateDeviceRGB();

    mainViewContentContext = CGBitmapContextCreate (NULL, width, height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); 
    CGColorSpaceRelease(colorSpace);

    if (mainViewContentContext==NULL)
        return NULL;

    CGRect subImageRect; 
    CGImageRef tileImage;
    float xCord=0;
    float yCord=0;
    subImageRect = CGRectMake(xCord,yCord,width,height);
    tileImage = CGImageCreateWithImageInRect(img,subImageRect);
    //UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:tileImage],nil,nil,nil);
    CGImageRef maskImage = maskImage1.CGImage;
    CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, width, height), maskImage);
    CGContextDrawImage(mainViewContentContext, CGRectMake(0, 0, width, height), tileImage);
    //UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage: maskImage],nil,nil,nil);
    CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);
    CGContextRelease(mainViewContentContext); 
    UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
    //UIImageWriteToSavedPhotosAlbum(theImage,nil,nil,nil);
    CGImageRelease(mainViewContentBitmapContext);
    return theImage;
}

AND THIS IS HOW I HAVE MOVED THE IMAGE

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    if([timer isValid])
        [timer invalidate];

    NSSet *allTouches = [event allTouches];

    switch ([allTouches count])
    {
        case 1: {
            //The image is being panned (moved left or right)
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
            CGPoint centerPoint = [touch locationInView:[self view]];

            [imgView setCenter:centerPoint];

            NSLog(@"Center.x=%f Center.y=%f",centerPoint.x,centerPoint.y);


        } break;
        case 2: {
            //The image is being zoomed in or out.

            UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
            UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];

            //Calculate the distance between the two fingers.
            CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]];
            NSLog(@"Initial=%f Final=%f",initialDistance,finalDistance);
            //Check if zoom in or zoom out.
            //Check if zoom in or zoom out.




            int width;
            int height;

            if(initialDistance < finalDistance) {
                NSLog(@"Zoom Out");

                zx=imgView.frame.origin.x-2.5;
                zy=imgView.frame.origin.y-2.5;
                width=imgView.frame.size.width+5;
                height=imgView.frame.size.height+5;

            }
            else
            {
                NSLog(@"Zoom In");



                zx=imgView.frame.origin.x+2.5;
                zy=imgView.frame.origin.y+2.5;
                               width=imgView.frame.size.width-5;
                               height=imgView.frame.size.height-5;

            }

            imgView.frame = CGRectMake(zx,zy,width,height);
            initialDistance = finalDistance;

        } break;
    }

}

hAPPY cODING...

Suriya