문제

사용자 정의 카메라를 구현하려고 시도하는 앱이 있습니다.이것은 소스 코드입니다 :

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도 왼쪽으로 회전합니다. 여기에 이미지 설명

다시 회전하려고 시도했습니다.

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;
.

내 이미지는 오른쪽에 회전하지만 미러링되고 스트레칭이 표시됩니다.

도움말?나는 다른 방법으로 회전 할 수 있거나, 나는 이것을 좋아하지 않는 사진을 찍을 수 있습니까?

도움이 되었습니까?

해결책

이 문제의 해결을 발견했습니다 :

i 방식으로 이미지를 회전하고 변형합니다

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;
.

다른 팁

i 또한 복사 및 붙여 넣기만으로도 동일한 문제가 발생할 수 있습니다.

이 코드를 사용해보십시오. 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;

}
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top