2012-07-26 172 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中的哪個分辨率。