问题
What I want to do is the following. A user can take a picture with the iPhone camera. After the picture is taking their will be another image placed on top of the taken picture. also the image that is placed on top should rotate a 25°. Besides the rotation I also need to lower that image on top a little bit below the taken picture. You can see what I mean over here.
I have the following.
-(UIImage *)drawImage:(UIImage*)profileImage withBadge:(UIImage *)badge
{
UIGraphicsBeginImageContextWithOptions(profileImage.size, NO, 0.0f);
[profileImage drawInRect:CGRectMake(0, 0, profileImage.size.width, profileImage.size.height)];
[badge drawInRect:CGRectMake(profileImage.size.width - badge.size.width, profileImage.size.height - badge.size.height,badge.size.width,badge.size.height)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
-(void)captureImage {
UIImage *img1 = [cropFilter imageFromCurrentlyProcessedOutput];
UIImage *img2 = [UIImage imageNamed:@"genkonstage.jpg"];
UIImage *img = [self drawImage:img1 withBadge:img2];
//UIImage *img = [cropFilter imageFromCurrentlyProcessedOutput];
[stillCamera.inputCamera unlockForConfiguration];
[stillCamera stopCameraCapture];
[self removeAllTargets];
staticPicture = [[GPUImagePicture alloc] initWithImage:img
smoothlyScaleOutput:YES];
Anybody can help me ?
回答1:
I would suggest something like this:
#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0)
-(UIImage *)drawImage:(UIImage*)profileImage withBadge:(UIImage *)badge
{
UIGraphicsBeginImageContextWithOptions(profileImage.size, NO, 0.0f);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextClearRect(imageContext, CGRectMake(0, 0, profileImage.size.width, profileImage.size.height)); // just in case let's clear the context
[profileImage drawInRect:CGRectMake(0, 0, profileImage.size.width, profileImage.size.height)];
float angle = DEGREES_TO_RADIANS(20);
CGContextSaveGState (imageContext); {
CGContextTranslateCTM(imageContext, profileImage.size.width - badge.size.width, profileImage.size.height - badge.size.height); // this will shift your context
CGAffineTransform rotationMatrix = CGAffineTransformMake(cos(angle), -sin(angle), sin(angle), cos(angle), 0, 0);
CGContextConcatCTM(imageContext, rotationMatrix); // this will rotate the context
// Now, having the context prepared (rotated and translated), draw the badge into it
[badge drawInRect:CGRectMake(0, 0, badge.size.width, badge.size.height)];
} CGContextRestoreGState (imageContext);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
This code is working for me.
来源:https://stackoverflow.com/questions/16062974/place-an-image-on-top-of-another-image