2012-07-26 80 views
3

我想让用户拍摄照片,然后显示灰度版本。但是,由于图像文件太大/分辨率太高,速度很慢。iOS:拍摄低质量图像

如何在用户拍摄照片时降低图像的质量?

继承人我使用了转换的代码:

- (UIImage *)convertImageToGrayScale:(UIImage *)image 
{ 
    // Create image rectangle with current image width/height 
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height); 
    // Grayscale color space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 
    // Create bitmap content with current image size and grayscale colorspace 
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone); 
    // Draw image into current context, with specified rectangle 
    // using previously defined context (with grayscale colorspace) 
    CGContextDrawImage(context, imageRect, [image CGImage]); 
    /* changes start here */ 
    // Create bitmap image info from pixel data in current context 
    CGImageRef grayImage = CGBitmapContextCreateImage(context); 
    // release the colorspace and graphics context 
    CGColorSpaceRelease(colorSpace); 
    CGContextRelease(context); 
    // make a new alpha-only graphics context 
    context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, nil, kCGImageAlphaOnly); 
    // draw image into context with no colorspace 
    CGContextDrawImage(context, imageRect, [image CGImage]); 
    // create alpha bitmap mask from current context 
    CGImageRef mask = CGBitmapContextCreateImage(context); 
    // release graphics context 
    CGContextRelease(context); 
    // make UIImage from grayscale image with alpha mask 
    UIImage *grayScaleImage = [UIImage imageWithCGImage:CGImageCreateWithMask(grayImage, mask) scale:image.scale orientation:image.imageOrientation]; 
    // release the CG images 
    CGImageRelease(grayImage); 
    CGImageRelease(mask); 
    // return the new grayscale image 
    return grayScaleImage; 
    /* changes end here */ 
} 
+0

你能发表一些代码吗?这很重要,因为它取决于您将灰度转换为灰度的方式。 – Stavash 2012-07-26 16:29:00

+0

不要认为重要......我只是想要一张质量较低的照片,但无论如何贴出来。 – SuperString 2012-07-26 16:32:45

+0

现在我们知道您正在拍摄UIImage并将其返回。 – Stavash 2012-07-26 16:37:05

回答

2

在将它传递给灰度转换之前,如何对UIImage进行降采样?例如:

NSData *imageAsData = UIImageJPEGRepresentation(imageFromCamera, 0.5); 
UIImage *downsampledImaged = [UIImage imageWithData:imageAsData]; 

您当然可以使用0.5以外的其他压缩品质。

+0

当我将质量改为高分辨率时,UIImageJPEGRepresentation(imageFromCamera,1.0);它需要更多时间或无法达到服务器的时间。 – 2013-08-30 06:06:54

2

如果使用AVFoundation捕获图像,你可以设置图像的质量,通过改变捕获会话预设像下面这样被捕获:

AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
session.sessionPreset = AVCaptureSessionPresetLow; 

有一个表格,它对应于AVFoundation Programming Guide中的哪个分辨率。