キャプチャボタンがあるビューを作成しました。そのボタンをクリックすると、カメラ画面が開きます。ただし、ポートレートで撮影した画像は、ランドスケープで表示すると歪んでしまいます。歪みのない実際の画像を取得するにはどうすればよいですか?
質問する
861 次
2 に答える
4
iOS 5で作業している場合は、画像をキャプチャして写真に保存するときに、この関数を手動で呼び出す必要があります。
- (UIImage *)fixOrientation : (UIImage *)img
{
if (img.imageOrientation == UIImageOrientationUp) return img;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (img.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, img.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
}
switch (img.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, img.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, img.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
}
CGContextRef ctx = CGBitmapContextCreate(NULL, img.size.width, img.size.height,
CGImageGetBitsPerComponent(img.CGImage), 0,
CGImageGetColorSpace(img.CGImage),
CGImageGetBitmapInfo(img.CGImage));
CGContextConcatCTM(ctx, transform);
switch (img.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage);
break;
}
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img1 = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img1;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
snapimage.image = [self fixOrientation:self.image];
}
- (void)imagePickerController:(UIImagePickerController *)picker1
didFinishPickingImage:(UIImage *)image1
editingInfo:(NSDictionary *)editingInfo
{
[picker1 dismissModalViewControllerAnimated:YES];
snapimage.image = [self fixOrientation:image1];
}
于 2012-04-09T09:37:37.217 に答える
2
shouldAutorotateToInterfaceOrientation
方法で、ビューの中心に横向きで画像をカスタマイズする必要があります
このようにしてみてください:
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationLandscapeLeft)
{
here customize the rect of your image
}
于 2012-04-09T09:36:11.710 に答える