将图片放在另一张图片的上方 [英] Place an image on top of another image

查看:95
本文介绍了将图片放在另一张图片的上方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有以下内容.

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

有人可以帮助我吗?

推荐答案

我建议这样:

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

此代码对我有用.

这篇关于将图片放在另一张图片的上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆