【问题标题】:Draw image constrain aspects绘制图像约束方面
【发布时间】:2012-02-16 13:44:15
【问题描述】:

我有一个 imageview,这个 imageview 中的图像供您从 photoroll 中选择。我还有一个按钮,当您单击此按钮时,会使用 addSubview 代码将图像添加到视图中。这幅图像是可拖动、可调整大小和可旋转的。

一个问题,当我完成图像时,我使用方法drawInRect。这会将所有图层相互绘制并创建图像。然而,这些层位于错误的位置并且大小错误。它也从不旋转。我不知道如何解决这个问题,这段代码在此文本下方。是否可以保持原始图像大小并且仍然将图层绘制在我将它们拖到图像视图上的同一个地方,如果不是,我如何为此创建一个新大小并获得我想要的结果。以及如何绘制旋转的图像。

UIGraphicsBeginImageContext(imageView2.image.size);    
// Draw image1
[imageView2.image drawInRect:CGRectMake(0, 0, imageView2.image.size.width, imageView2.image.size.height)];    
// Draw image2
for(UIImageView *viewsSub in [self.imageViewer subviews])
{            
    [viewsSub.image drawInRect:CGRectMake(viewsSub.frame.origin.x, viewsSub.frame.origin.y, viewsSub.frame.size.width, viewsSub.frame.size.height)];
}    
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();    
pld.imageChosen2 = resultingImage;    
UIGraphicsEndImageContext();

【问题讨论】:

    标签: iphone cocoa-touch uiimageview uiimage


    【解决方案1】:

    因此,您想要拍摄实际图像视图的“屏幕截图”(包括子视图),不是吗?

    我用这段代码做了类似的事情,但不知道是否适合你。

         - (UIImage *)screenshot {
            CGFloat scale = [UIScreen screenScale];
    
            if(scale > 1.5) {
                UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, scale);
            } else {
                UIGraphicsBeginImageContext(self.frame.size);
            }
            [self.layer renderInContext:UIGraphicsGetCurrentContext()];
            UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return screenshot; 
    }
    

    您应该在您的 imageview(包含您要添加的所有子视图的那个)中添加此方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      相关资源
      最近更新 更多