我正在使用相机选择器控制器来拍照并在我的应用中显示它。但是我没有使用UIImageView
来显示图片。我在UIImage
上绘制UIView
,我得到的结果是反转图像。这是截图:
我点击使用照片后的结果是:
这是我的代码:
- (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);
}
我尝试CGAffineTransformMakeRotation
和CGContextRotateCTM
正确显示图像,但是它们会旋转图像,而不是按正确的顺序显示内容。如您所见,图像中的内容完全相反。
如何绘制uiimage
而不对其外观进行任何更改?
答案 0 :(得分: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];
}
UIImageView
作为drawView
的子视图。让图像视图完成正确渲染图像所需的所有工作。