2012-03-12 2 views
2

У меня есть приложение, в котором я пытался реализовать пользовательскую камеру. Это исходный код:Пользовательская камера iOS

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 

    captureVideoPreviewLayer.frame = self.vImagePreview.bounds; 
    [self.vImagePreview.layer addSublayer:captureVideoPreviewLayer]; 

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    //device.position ; 
    NSError *error = nil; 
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
    [session addInput:input]; 
    [session startRunning]; 

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; 
    [stillImageOutput setOutputSettings:outputSettings]; 

    [session addOutput:stillImageOutput]; 

Затем я попытался создать снимок и отправить его на другой контроллер вида:

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

    //NSLog(@"about to request a capture from: %@", stillImageOutput); 

    AcceptingPhotoViewController *photo = [[AcceptingPhotoViewController alloc] initWithNibName:@"AcceptingPhotoViewController" bundle:nil]; 

    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) 
    { 
     CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); 

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
     UIImage *image = [[UIImage alloc] initWithData:imageData]; 

     photo.image = [[UIImage alloc] init ]; 
     photo.image = image; 
     photo.photoFromCamera = YES; 

     [self.navigationController pushViewController:photo animated:NO]; 
    }]; 
} 

Но в моем целевом классе это изображение вращается влево на 90 градусов: enter image description here

Я попытался повернуть его обратно:

float newSide = MAX([image size].width, [image size].height); 
        CGSize size = CGSizeMake(newSide, newSide); 
        UIGraphicsBeginImageContext(size); 
        CGContextRef ctx = UIGraphicsGetCurrentContext(); 
        CGContextTranslateCTM(ctx, newSide/2, newSide/2); 
        CGContextRotateCTM(ctx, 1.57079633); 
        CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage); 
        UIImage *i = UIGraphicsGetImageFromCurrentImageContext(); 
        UIGraphicsEndImageContext(); 
        image = i; 

Мое изображение повернуто справа, но оно отображается зеркально и растягивается.

Любая помощь? Могу ли я повернуть другим способом, или, может быть, я должен сделать фотографию не так?

ответ

3

Я нашел решение этой проблемы:

Я вращать и преобразовать изображение таким образом

UIView* rotatedViewBox = [[UIView alloc] initWithFrame: CGRectMake(0, 0, image.size.width, image.size.height)]; 
        float angleRadians = 90 * ((float)M_PI/180.0f); 
        CGAffineTransform t = CGAffineTransformMakeRotation(angleRadians); 
        rotatedViewBox.transform = t; 
        CGSize rotatedSize = rotatedViewBox.frame.size; 

       UIGraphicsBeginImageContext(rotatedSize); 
       CGContextRef bitmap = UIGraphicsGetCurrentContext(); 
       CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 
       CGContextRotateCTM(bitmap, angleRadians); 

       CGContextScaleCTM(bitmap, 1.0, -1.0); 
       CGContextDrawImage(bitmap, CGRectMake(-image.size.width/2, -image.size.height/2, image.size.width, image.size.height), [image CGImage]); 

       UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
       UIGraphicsEndImageContext(); 
       image = newImage; 

       CGSize newSize = CGSizeMake(image.size.height, image.size.width); 
       UIGraphicsBeginImageContext(newSize); 

       // Tell the old image to draw in this new context, with the desired 
       // new size 
       [image drawInRect:CGRectMake(0,0,image.size.height,image.size.width)]; 

       // Get the new image from the context 
       UIImage* newImage2 = UIGraphicsGetImageFromCurrentImageContext(); 

       // End the context 
       UIGraphicsEndImageContext(); 

       image = newImage2; 
0

Я также с той же проблемой просто скопировать и вставить.

Попробуйте этот код он будет помогает U ..

- (UIImage *)fixrotation:(UIImage *)image{ 
    if (image.imageOrientation == UIImageOrientationDown) return image; 
CGAffineTransform transform = CGAffineTransformIdentity; 

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

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

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

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

    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRightMirrored: 
     transform = CGAffineTransformTranslate(transform, image.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, image.size.width, image.size.height, 
             CGImageGetBitsPerComponent(image.CGImage), 0, 
             CGImageGetColorSpace(image.CGImage), 
             CGImageGetBitmapInfo(image.CGImage)); 
CGContextConcatCTM(ctx, transform); 
switch (image.imageOrientation) { 
    case UIImageOrientationLeft: 
    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRight: 
    case UIImageOrientationRightMirrored: 
     // Grr... 
     CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage); 
     break; 

    default: 
     CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.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; 

} 
Смежные вопросы