Pregunta

I must capture still image with cocoa touch AVFoundation framework,but I found that captured image be stretched. I have configured the captureSession as these:

[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]]];
self.captureSession.sessionPreset=AVCaptureSessionPresetHigh;
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

And use these code to capture image:

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{

    if(error)
        NSLog(@"error=%@",error);
    else
    {
        if (imageDataSampleBuffer != NULL) {
            NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image=[[UIImage alloc] initWithData:imageData];

            CGRect screenRect = [[UIScreen mainScreen] bounds];
            CGFloat screenWidth = screenRect.size.width;
            CGFloat screenHeight = screenRect.size.height;
            CGSize screenSize=CGSizeMake(screenWidth, screenHeight);
            NSLog(@"screen.width=%f,screen.height=%f",screenWidth,screenHeight);
            UIGraphicsBeginImageContextWithOptions(screenSize, NO, 0.0f);
            // This is where we resize captured image
            [(UIImage *)image drawInRect:CGRectMake(0, 0, screenWidth, screenHeight)];
            CGSize imageSize=[ImageUtility getImageSize:image];
            NSLog(@"image.width=%f,image.height=%f",imageSize.width,imageSize.height);
            // Save the results directly to the image view property
            UIImage *toSaveImage=UIGraphicsGetImageFromCurrentImageContext();
            imageSize=[ImageUtility getImageSize:toSaveImage];
            NSLog(@"combine.width=%f,combine.height=%f",imageSize.width,imageSize.height);
            UIGraphicsEndImageContext();
            UIImageWriteToSavedPhotosAlbum(toSaveImage, nil, nil, nil);
        }
    }
        }];
}

This result output:

013-08-29 09:30:06.182 WatermarkCamera[6882:907]        screen.width=320.000000,screen.height=480.000000
2013-08-29 09:30:07.208 WatermarkCamera[6882:907] image.width=1280.000000,image.height=720.000000
2013-08-29 09:30:07.238 WatermarkCamera[6882:907] combine.width=640.000000,combine.height=960.000000

The result output image stretched.How to fix the stretched image?Any suggestions should be appreciated.

¿Fue útil?

Solución

I think I shouldn't draw image with the size of screen.I should draw image with the size of image. I have defined new method to scale image to new width

+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width;

Now I called this method

UIImage *newImage=[ImageUtility imageWithImage:image scaledToWidth:screenWidth];
[newImage drawInRect:CGRectMake(0, 0, newImage.size.width, newImage.size.height)];

Finally write the new image to photo album.The new image doesn't stretch.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top