0

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

在此处输入图像描述

我有以下。

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

任何人都可以帮助我吗?

4

1 回答 1

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

这段代码对我有用。

于 2013-04-17T15:58:19.840 回答