将图像放在另一个图像的顶部

时间:2013-04-17 14:36:18

标签: iphone ios objective-c uiimageview uiimage

我想做的是以下内容。用户可以使用iPhone相机拍照。在拍摄照片后,将拍摄照片放在拍摄照片上方。放置在顶部的图像也应旋转25°。除了旋转之外,我还需要将图像降低到拍摄照片下方的一点点。 你可以在这里看到我的意思。

enter image description here

我有以下内容。

-(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];

任何人都可以帮助我?

1 个答案:

答案 0 :(得分:1)

我会建议这样的事情:

#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;
}

此代码对我有用。