【问题标题】:UIImage is reversed when drawn on UIView在 UIView 上绘制时 UIImage 反转
【发布时间】:2015-10-23 14:26:59
【问题描述】:

我正在使用相机选择器控制器拍照并将其显示在我的应用程序中。但是我没有使用UIImageView 来显示图片。我在UIView 上绘制UIImage,得到的结果是反转的图像。这是截图:

我点击使用照片后的结果是:

这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

    dView=[[drawView alloc]initWithFrame:imageUIView.frame];
    [dView drawImage:chosenImage];

    [imageUIView addSubview:dView];

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

在 dView 中:

-(void)drawImage:(UIImage *) imageToDraw{
    self.pic=imageToDraw;
    [imageToDraw drawInRect:self.frame];
    [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();

CGRect targetBounds = self.layer.bounds;
    // fit the image, preserving its aspect ratio, into our target bounds
CGRect imageRect = AVMakeRectWithAspectRatioInsideRect(self.pic.size,
                                                           targetBounds);
    // draw the image
CGContextDrawImage(context, imageRect, self.pic.CGImage);

// Save the screen to restore next time around
imageRef = CGBitmapContextCreateImage(context);

}

我尝试CGAffineTransformMakeRotationCGContextRotateCTM 正确显示图像,但它们会旋转图像,而不是使内容以正确的顺序显示。如您所见,图像中的内容完全相反。

如何在不改变外观的情况下绘制uiimage

【问题讨论】:

  • @picciano 为什么?不相关。

标签: ios objective-c uiview uiimage cgcontextdrawimage


【解决方案1】:

您的 drawView 类未正确绘制图像。试试这个:

- (void)drawImage:(UIImage *)imageToDraw {
    self.pic = imageToDraw;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CGRect targetBounds = self.layer.bounds;
        // fit the image, preserving its aspect ratio, into our target bounds
    CGRect imageRect = AVMakeRectWithAspectRatioInsideRect(self.pic.size,
                                                               targetBounds);
        // draw the image
    [self.pic drawInRect:imageRect];
}
  1. 最好添加UIImageView 作为drawView 的子视图。让图像视图完成正确渲染图像所需的所有工作。
  2. 命名约定。类名应该以大写字母开头。变量和方法名称以小写字母开头。

【讨论】:

  • 我可以使用 imageview 但是,我还有其他与通过触摸清除图层相关的东西。我无法在 imageview 上执行此操作,因此我使用的是 uiview。无论如何我会试试这个!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多