合并两个旋转的UIImage&缩放

时间:2012-02-21 06:05:47

标签: ios

我正在做一个这样的应用程序:你加载一张照片,你把图像放在上面,比如气球等。

当我尝试将其中一个图像合并到仅调整大小时,它可以正常工作。比10px更应该但不是问题。

当您旋转图像时出现问题[UIImageView]它看起来比图像大得多,我尝试分配的东西,什么都没有。我留下了代码。我希望有人可以提供帮助。

注意:图像大小在UIImageView内部,然后乘以主图像的比例

- (UIImage *)mergeImage:(UIImageView *)mainImage withImageView:(UIImageView *)imageView {

    UIImage *temp = imageView.image;
    UIImage *tempMain = mainImage.image;

    CGFloat mainScale = [self imageViewScaleFactor:mainImage];
    CGFloat tempScale = 1/mainScale;
    NSLog(@"%f", tempScale);

    //Rotate UIIMAGE
    UIGraphicsBeginImageContext(temp.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformTranslate(transform, temp.size.width/2, temp.size.height/2);    
    CGFloat angle = atan2(imageView.transform.b, imageView.transform.a);    
    transform = CGAffineTransformRotate(transform, angle);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    CGContextConcatCTM(ctx, transform);
    // Draw the image into the context
    CGContextDrawImage(ctx, CGRectMake(-temp.size.width/2, -temp.size.height/2, temp.size.width, temp.size.height), temp.CGImage);
    // Get an image from the context
    temp = [UIImage imageWithCGImage: CGBitmapContextCreateImage(ctx)];


    NSLog(@"%f %f %f", mainScale, mainImage.frame.size.width, mainImage.frame.size.height);

    UIGraphicsBeginImageContextWithOptions(tempMain.size, NO, 1.0f);

    //Get imageView size & position 
    NSLog(@"%f %f %f %f", imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height);

    CGFloat offsetX = 0;
    CGFloat offsetY = -44;

    if (tempMain.size.height > tempMain.size.width) {
        offsetX = ((tempMain.size.width * mainScale) - 320)/2;
    }else{
        offsetY = ((tempMain.size.height * mainScale) - 416)/2;
        offsetY -= 44;
    }

    CGFloat imageViewX = (imageView.frame.origin.x + offsetX) * tempScale;
    CGFloat imageViewY = (imageView.frame.origin.y + offsetY) * tempScale;
    CGFloat imageViewW = imageView.frame.size.width * tempScale;
    CGFloat imageViewH = imageView.frame.size.height * tempScale;


    CGRect tempRect = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);


    [tempMain drawAtPoint:CGPointZero];
    [temp drawInRect:tempRect];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

由于

1 个答案:

答案 0 :(得分:0)

这是适合我的解决方案

Merging a previosly rotated by gesture UIImageView with another one. WYS is not WYG

我只是将照片拍摄到主屏幕,然后将其裁剪为照片的大小,更快,更干净。如果应用程序在普通设备的视网膜中运行,那么分辨率就不错了。而且您需要准备该代码才能在视网膜和视网膜中工作。非视网膜