简体   繁体   English

在UIView上绘制时UIImage反转

[英]UIImage is reversed when drawn on UIView

I am using camera picker controller to take a picture and I display it in my app. 我正在使用相机选择器控制器拍摄照片,并将其显示在我的应用程序中。 However I am not using UIImageView to display the picture. 但是我不使用UIImageView显示图片。 I am drawing the UIImage on a UIView , and the result I get is the reversed image. 我在UIView上绘制UIImage ,得到的结果是反转的图像。 Here is the screenshot: 这是屏幕截图:

在此处输入图片说明

and the result after I click on use photo is: 我点击使用照片后的结果是:

在此处输入图片说明

Here is my code: 这是我的代码:

- (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];

}

and in dView: 并在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);

}

I tried CGAffineTransformMakeRotation and CGContextRotateCTM to display the image properly, but they rotate the image, instead of making the contents display in correct order. 我尝试使用CGAffineTransformMakeRotationCGContextRotateCTM正确显示图像,但是它们旋转图像,而不是按正确的顺序显示内容。 As you can see the contents in the image are totally reversed. 如您所见,图像中的内容完全相反。

How to draw the uiimage without making any changes to its appearence ? 如何在不更改外观的情况下绘制uiimage

Your drawView class isn't drawing the image properly. 您的drawView类无法正确绘制图像。 Try this: 尝试这个:

- (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. It may be better to add a UIImageView as a subview of your drawView . 最好将UIImageView添加为drawView的子视图。 Let the image view do all the needed work to render the image properly. 让图像视图完成所有必要的工作以正确渲染图像。
  2. Naming conventions. 命名约定。 Class names should start will uppercase letters. 类名应以大写字母开头。 Variable and method names start with lowercase letters. 变量和方法名称以小写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM