2013-08-29 66 views
0

我必须捕获静止图像与可可触摸AVFoundation框架,但我发现捕获的图像被拉伸。 我已经配置了captureSession因为这些:捕获静止图像与可可触摸AVFoundation框架

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

并使用这些代码来捕获图像:

[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); 
     } 
    } 
     }]; 
} 

该结果输出:

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 

结果输出图像stretched.How到修复拉伸的图像?任何建议,应该感激。

回答

0

我想我不应该画画面大小的画面。我应该画图像的大小。 我已经定义了新的方法来缩放图像以新的宽度

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

现在,我称这种方法

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

最后写入到照片album.The新形象新的图像不拉伸。