2016-12-01 13 views
2

画像を切り取った後に画像の回転が問題になります。私は現在使用しているステップバイステップの方法を以下に示します。ここでクロップ後にカスタムカメラから取り込まれた画像が回転します。

私は結果を得るために、その後のプロセスである:

手順:1 カスタムカメラを通して撮る写真

- (void)captureImageWithCompletionHander:(void (^)(id))completionHandler{ 
    [_cameraOverlayView hideHightLightOverlay]; 
    if (_isCapturing) return; 

    __weak typeof(self) weakSelf = self; 
    [weakSelf hideGLKView:YES completion:^{ 
     [weakSelf hideGLKView:NO completion:^{ 
      [weakSelf hideGLKView:YES completion:nil]; 
     }]; 
    }]; 
    _isCapturing = YES; 

    AVCaptureConnection *videoConnection = nil; 
    for (AVCaptureConnection *connection in self.stillImageOutput.connections){ 
     for (AVCaptureInputPort *port in [connection inputPorts]){ 
      if ([[port mediaType] isEqual:AVMediaTypeVideo]){ 
       videoConnection = connection; 
       break; 
      } 
     } 
     if (videoConnection) break; 
    } 

    [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error){ 
     if(!CMSampleBufferIsValid(imageSampleBuffer)){ 
      return; 
     } 
     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 

     UIImage * capturedImage = [[UIImage alloc]initWithData:imageData scale:1]; 
     NSLog(@"%lu",(unsigned long)UIImageJPEGRepresentation(capturedImage, 1.0).length); 
     if (_myDevice == [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0]) { 
      capturedImage = capturedImage; 
     }else if (_myDevice == [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][1]) { 
     } 

     [weakSelf hideGLKView:NO completion:nil]; 
     completionHandler(capturedImage); 
     _isCapturing = NO; 
    }]; 
} 

ステップ:2修正の向き

-(id)initWithImage:(UIImage*) image{ 
    self = [super init]; 
    if (self){ 
     self.originalImage = [image fixOrientation]; 
    } 

    return self; 
} 

//Method for fix orientation 
@implementation UIImage (fixOrientation) 

- (UIImage *)fixOrientation { 

    // No-op if the orientation is already correct 
    if (self.imageOrientation == UIImageOrientationUp) return self; 

    // We need to calculate the proper transformation to make the image upright. 
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 
    CGAffineTransform transform = CGAffineTransformIdentity; 

    switch (self.imageOrientation) { 
     case UIImageOrientationDown: 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
      transform = CGAffineTransformRotate(transform, M_PI_2); 
      break; 

     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      transform = CGAffineTransformTranslate(transform, 0, self.size.height); 
      transform = CGAffineTransformRotate(transform, -M_PI_2); 
      break; 
     case UIImageOrientationUp: 
     case UIImageOrientationUpMirrored: 
      break; 
    } 

    switch (self.imageOrientation) { 
     case UIImageOrientationUpMirrored: 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 

     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRightMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.height, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 
     case UIImageOrientationUp: 
     case UIImageOrientationDown: 
     case UIImageOrientationLeft: 
     case UIImageOrientationRight: 
      break; 
    } 

    // Now we draw the underlying CGImage into a new context, applying the transform 
    // calculated above. 
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, 
              CGImageGetBitsPerComponent(self.CGImage), 0, 
              CGImageGetColorSpace(self.CGImage), 
              CGImageGetBitmapInfo(self.CGImage)); 
    CGContextConcatCTM(ctx, transform); 
    switch (self.imageOrientation) { 
     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      // Grr... 
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); 
      break; 

     default: 
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); 
      break; 
    } 

    // And now we just create a new UIImage from the drawing context 
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
    UIImage *img = [UIImage imageWithCGImage:cgimg]; 
    CGContextRelease(ctx); 
    CGImageRelease(cgimg); 
    return img; 
} 
@end 

ステップ:3クロップ画像

-(UIImage *)cropImage:(UIImage *)image{ 
    CGFloat overlayHeight = self.frame.size.height; 
    UIImageOrientation originalOrientation = image.imageOrientation; 
    CGFloat originalScale = image.scale; 
    CIImage *rawImage = [CIImage imageWithCGImage:image.CGImage]; 

    NSMutableDictionary *rectangleCoordinates = [NSMutableDictionary new]; 
    rectangleCoordinates[@"inputTopLeft"] = [CIVector vectorWithCGPoint:CGPointMake(topLeftPath.x * _absoluteWidth, (overlayHeight - topLeftPath.y) * _absoluteHeight)]; 
    rectangleCoordinates[@"inputTopRight"] = [CIVector vectorWithCGPoint:CGPointMake(topRightPath.x * _absoluteWidth, (overlayHeight - topRightPath.y) * _absoluteHeight)]; 
    rectangleCoordinates[@"inputBottomLeft"] = [CIVector vectorWithCGPoint:CGPointMake(bottomLeftPath.x * _absoluteWidth, (overlayHeight - bottomLeftPath.y) * _absoluteHeight)]; 
    rectangleCoordinates[@"inputBottomRight"] = [CIVector vectorWithCGPoint:CGPointMake(bottomRightPath.x * _absoluteWidth, (overlayHeight - bottomRightPath.y) * _absoluteHeight)]; 
    rawImage = [rawImage imageByApplyingFilter:@"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates]; 

    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef cgImage = [context createCGImage:rawImage fromRect:[rawImage extent]]; 
    UIImage *RImage = [UIImage imageWithCGImage:cgImage scale:originalScale orientation:originalOrientation]; 
    return RImage; 
} 

結果:

  • は、画像をキャプチャした後、私はそれがまっすぐに見える&元の画像を示しています。問題ありません。

参照イメージ: enter image description here

  • しかし、トリミングした後、それは

参照イメージ回転します: enter image description here

私はすでに試みたが、働いていないいくつかの参照:

Using CoreImage to filter an image results in image rotation

iOS UIImagePickerController result image orientation after upload

iOS - UIImageView - how to handle UIImage image orientation

How to get the Correct orientation of the image selected from the Default Image gallery

Convert a UIImage to a CIImage to crop to a CGRect. AVFoundation

誰かがこの問題を解決するために導く場合、それは素晴らしいことでしょう。

ありがとうございます。

+0

cropingのためにこれを試して.. - (UIImage *)作物:(CGRect)RECTを{ RECT = CGRectMake(rect.origin.x * self.scale、 rect.origin.y * self.scale、 RECT .size.width * self.scale、 rect.size.height * self.scale); CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage]、rect); UIImage * result = [UIImage imageWithCGImage:imageRef スケール:self.scale orientation:self.imageOrientation]; CGImageRelease(imageRef);戻り値: ; } –

+0

私はあなたが私のプロジェクトでこの答えを使用し、私のprorblemを解決するイメージを作ってこの答えを参照する必要があると思います.. http://stackoverflow.com/questions/158914/cropping-an-uiimage –

+0

http:// stackoverflow。 com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload上記のリンクに従います – arvind

答えて

0

、最終的に私はUIBeizerPathに移動することによってそれを解決しました。

オリエンテーションの問題は、決して解決されないCIImageが原因であることに気付きました。 CIImageという理由だけで、元の画像が正しく切り取られていたとしても、クロップ後に回転していました。だからここUIBeizerPath &で試してみました

は私がやったことです:

UIBezierPath *path = [_overlayView getPath]; 
CGRect rect = CGRectZero; 
rect.size = _detectedImage.frame.size; 

CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.frame = CGRectMake(0, 0, _detectedImage.frame.size.width, _detectedImage.frame.size.height); 
shapeLayer.path = path.CGPath; 
shapeLayer.fillColor = [[UIColor whiteColor] CGColor]; 
shapeLayer.backgroundColor = [[UIColor clearColor] CGColor]; 
[_detectedImage.layer setMask:shapeLayer]; 

UIGraphicsBeginImageContextWithOptions(_detectedImage.layer.frame.size, NO, 0); 
[_detectedImage.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIImage *cpImage = [outputImage imageByTrimmingTransparentPixelsRequiringFullOpacity:YES]; 
UIGraphicsEndImageContext(); 

は、それが誰かを助けることを願っています。

1

'cropImage'メソッドの次の行にある向きをUIImageOrientationUpに置き換えてください。非常に多くのソリューションをしようとして非常に多くの実験&後

UIImage *RImage = [UIImage imageWithCGImage:cgImage scale:originalScale orientation:UIImageOrientationUp]; 
+0

それを試しても動作しません – Mayur

関連する問題