1

我有一个应用程序,我在其中将图像(例如顶部图像)放在相机叠加图像上。相机overlayView上的图像可以缩放,捏合和旋转,然后将其与相机图像结合。我在stackoverflow上找到了一个代码,通过它我成功地旋转了图像。但是当旋转顶部图像后结合起来得到缩放并且某些部分也被裁剪了。请帮忙。谢谢我提前。我的代码是。

-(UIImage*)combineImage:(UIImage *)camImage withImage:(UIImage *)topImage
{

        UIImage *scaledTopImg = [self imageByScalingProportionallyToSize:imageView.frame.size withImage:topImage];

        UIGraphicsBeginImageContext(scaledTopImg.size); //scaling top image after resizing
        CGContextRef ctx = UIGraphicsGetCurrentContext();     
        CGContextTranslateCTM(ctx, imageView.frame.size.width*0.5f , imageView.frame.size.height*0.5f);                    

        CGFloat angle = atan2(imageView.transform.b, imageView.transform.a);
        CGContextRotateCTM(ctx, angle);

        [scaledTopImg drawInRect:CGRectMake(- scaledTopImg.size.width * 0.5f, -(scaledTopImg.size.height  * 0.5f), scaledTopImg.size.width, scaledTopImg.size.height)];

        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        if ([[UIDevice currentDevice]orientation]==UIInterfaceOrientationLandscapeLeft ||[[UIDevice currentDevice]orientation]==UIInterfaceOrientationLandscapeRight) {
            UIGraphicsBeginImageContext(CGSizeMake( 1024,768));//Creating Camera image
            [camImage drawInRect:CGRectMake(0, 0, 1024, 768)];
        }
        else{
            UIGraphicsBeginImageContext(CGSizeMake(768, 1024));
            [camImage drawInRect:CGRectMake(0, 0, 768, 1024)];
        }

        CGRect rect = imageView.frame;

        [newImage drawInRect:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,imageView.frame.size.width, imageView.frame.size.height)]; //Drawing scaled image over camera image 

        UIImage *newImage2 = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage2;
}
4

0 回答 0