从uiimagePickerController中选择的石英拉伸图像

时间:2009-06-03 21:24:27

标签: iphone cocoa-touch quartz-graphics uiimagepickercontroller

我有一个应用程序,允许用户从相机或照片库中选择一个图像。然后它将该图像放在ImageView中。然后我使用Quartz从图像视图中抓取图像并将其与一些基本叠加等一起渲染。

问题在于,从相机或相机胶卷中拾取图像时,Quartz会以90度旋转绘制图像并垂直拉伸图像。 从照片库中选择任何其他照片时没有问题。如果allowsImageEditing设置为YES,则不会出现此问题。

如果有人可以解决它会有所帮助,我就是这方面的新手。

我的CameraViewController中的代码

#pragma mark -
- (IBAction)getCameraPicture {
     UIImagePickerController *picker = [[UIImagePickerController alloc] init];
     picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
     picker.delegate = self;
     picker.allowsImageEditing = NO; //seems to fix the weird rotation & stretching issues if set to YES
     picker.sourceType = UIImagePickerControllerSourceTypeCamera;
     [self presentModalViewController:picker animated:YES];
     //[picker release];
}
- (IBAction)selectExistingPicture {
     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
     UIImagePickerController *picker = [[UIImagePickerController alloc] init];
     picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
     picker.delegate = self;
     picker.allowsImageEditing = NO; //seems to fix the weird rotation & stretching issues if set to YES (only with camera roll)
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     [self presentModalViewController:picker animated:YES];
     //[picker release];
     }
}
#pragma mark -
- (void)imagePickerController:(UIImagePickerController *)picker 
 didFinishPickingImage:(UIImage *)image
   editingInfo:(NSDictionary *)editingInfo {

     imageView.image = image;
     NSLog(@"imagePicker image size %.0f x %.0f", image.size.width, image.size.height);
     [picker dismissModalViewControllerAnimated:YES];
     [picker release];
     [quartzContextView setNeedsDisplay];
     [self setUpAndRenderImage];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
     [picker dismissModalViewControllerAnimated:YES];
     [picker release];
}

绘制Quartz的视图中的代码

NSLog(@"Quartz image size %.0f x %.0f", drawImageTwo.size.width, drawImageTwo.size.height);
if (drawImageTwo.size.width >= drawImageTwo.size.height)
{
     scaleFactor = (insideHeight+5)/drawImageTwo.size.height;
     xOffset = (drawImageTwo.size.width*scaleFactor - insideWidth)/2;
     yOffset = 0;
     NSLog(@"Image is Landscaepe");
}
else if (drawImageTwo.size.width < drawImageTwo.size.height)
{
     scaleFactor = (insideWidth+5)/drawImageTwo.size.width;
     yOffset = (drawImageTwo.size.height*scaleFactor - insideHeight)/2;
     xOffset = 0;
     NSLog(@"Image is Portrait");
}

//draw Photo Base
CGRect baseRect = CGRectMake(0.0, 0.0, width, height);
CGContextDrawImage (context, baseRect, drawImage.CGImage);

//clip all further drawing to context clip
CGContextClipToRect(context, CGRectMake(leftOffset, (height-insideHeight-topOffset), insideWidth, insideHeight));

//draw Photo
CGRect photoRect = CGRectMake(leftOffset-xOffset, (height-insideHeight-topOffset)-yOffset, drawImageTwo.size.width*scaleFactor, drawImageTwo.size.height*scaleFactor);
CGContextDrawImage (context, photoRect, drawImageTwo.CGImage);

1 个答案:

答案 0 :(得分:0)

您遇到的问题来自UIKit和CoreGraphics中不同的坐标系统和图像方向处理。

UIImage知道它的方向,即第一次拍摄图像时相机的方向。只要您使用UIKit Classes,一切都按预期工作,但是当您获得UIImage的原始CGImage时,您将丢失此信息。您必须通过转换CGContext的CTM(当前转换矩阵)来说明您自己的图像方向。

但CoreGraphics使用的坐标系不够,原点位于左下角的角落,而UIKit的原点位于左上角。请参见Quartz 2D编程指南章节“图形上下文”。你必须将CTM缩放0,-1并将其翻译为高度以便考虑到这一点。

在你的例子中,因为你没有对图像做过花哨的事情,所以尝试使用UIImages drawInRect:方法似乎是最合适的,它已经解决了这两个问题。