我正在使用相机选择器控制器拍照并将其显示在我的应用程序中。但是我不用UIImageView
来显示图片。我UIImage
在 a 上绘制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
不改变外观的情况下绘制?