【问题标题】:Draw Image After scale and rotate it, to other image iOS缩放并旋转后绘制图像到其他图像iOS
【发布时间】:2012-11-13 19:23:47
【问题描述】:

我有一个比原始图像更小的图像的 UIImageView。然后我将许多其他 UIImageView 作为子视图添加到主图像视图中。我还缩放和旋转许多子视图(imageview)。现在我想从原始图像创建图像,所有子视图图像都附加在 imageview 上,但保持位置。我的问题是如何在缩放后将许多子视图图像绘制成原始图像并旋转它们。我使用了 CGContextRotateCTM 但似乎它旋转太多了。请帮我 我的示例代码是

CGContextRef context = UIGraphicsGetCurrentContext();
[originalImage drawAtPoint:CGPointZero];
for(UIView *aView in [self.mainImageView subviews]){
    float rate = 1000/self.mainImageView.frame.size.width;
    if(aView.tag != DELETE_ATTACH_IMAGEVIEW_TAG && aView.tag != ROTATE_ZOOM_IMAGEVIEW_TAG){
        CGRect rect = CGRectMake(aView.frame.origin.x*rate, aView.frame.origin.y*rate, aView.frame.size.width *rate, aView.frame.size.height*rate);
        if([aView isKindOfClass:[CustomImageView class]]){
            CustomImageView *temp = (CustomImageView *)aView;
            float rotation = [temp rotation];
            UIImage *tempImage= temp.image;
            CGContextRotateCTM(context, rotation);
            [tempImage drawInRect:rect];
        }

    }

}
originalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【问题讨论】:

  • 我不确定我是否正确理解了您的问题。您的主视图是否包含所有旋转的子视图?还是您只是想在另一个视图之上绘制一些视图?
  • 你想要什么??要将当前视图保存为目录中的图像??
  • 不,例如我有大小为 (1000, 1000) 的原始图像,但在 UIImageView 中我将原始图像调整为新大小 100 x 100,然后我将许多小图像添加到 UIImageView(大小 100x100) .现在我想将所有小图像(子视图)绘制到大小为 1000x1000 且位置相同的原始图像

标签: iphone objective-c ios image uiimage


【解决方案1】:

只需捕获整个视图,然后使用整个图像中的一个图像

- (UIImage *)captureView {

   //hide controls if needed
    CGRect rect = [self.view bounds];

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}

捕获此图像后,将其设置为背景,如果不需要,则删除其他所有图像..

【讨论】:

  • 感谢您的帮助,但我想要原始图像的全尺寸,我曾经尝试过您的代码,如果我更改上下文大小,我会在大图像中得到小图像
  • @QuangPhạmCông 嘿,伙计,我不明白你想要什么,但如果你只想捕捉背景图像或背景的完整图像,那么使用背景图像的边界,如果你不想捕捉,还可以隐藏一些图像或控件与原始图像..我希望你理解它伴侣.. :)]
  • 我只想将 uiimage 视图及其上的所有子视图保存为 1 张图像,但分辨率高,尺寸比 uiimageview 框架大
  • 哦,好的,伙计,如果我能解决您的这个要求,那么我会发布代码好的伙计.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
相关资源
最近更新 更多