2015-08-15 47 views
0

與UIBezierPath我裁剪UIImages使用UIGraphicsContext(像素格式,編解碼器):UIImage的扭曲。UIGraphicsBeginImageContext處理更大的文件

CGSize thumbnailSize = CGSizeMake(54.0f, 45.0f); // dimensions of UIBezierPath 
UIGraphicsBeginImageContextWithOptions(thumbnailSize, NO, 0); 
[path addClip]; 
[originalImage drawInRect:CGRectMake(0, originalImage.size.height/-3, thumbnailSize.width, originalImage.size.height)]; 
UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

但由於某些原因我的圖片越來越垂直拉伸(一切看起來稍微長和瘦),而且這種效果越大,我的originalImage就越大。我確定原始圖像在我做這些操作之前是完全正常的(我已經檢查過)

如果那麼重要的話,我的圖像都是9:16(說72px寬,128px高)。

我見過UIGraphics使用「主機字節順序」的ARGB 32位整數像素格式創建位圖;我承認在涉及像素格式時有些無知,但是覺得這可能是相關的,因爲我不確定這是否與用於編碼圖像數據的像素格式相同。

不知道如何相關,這是但這裏是完整的加工流水線: 我使用AVFoundation捕獲和設置我photoSettings爲

NSDictionary *photoSettings = @{AVVideoCodecKey : AVVideoCodecH264}; 

使用captureStillImageAsynchronouslyFromConnection:..然後使用[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];把它變成NSData的捕捉,然後瘦身通過創建CGDataProviderRefWithCFData並轉換爲CGImageRef使用CGImageSourceCreateThumbnailAtIndex並從中獲取UIImage來創建縮略圖。

後來,我再次把它變成NSData使用UIImageJPEGRepresentation(thumbnail, 0.7),所以我可以存儲。最後,當我準備好顯示時,我打電話給我自己的方法,詳細地列在[self maskImage:[UIImage imageWithData:imageData] toPath:_thumbnailPath]上,並將其顯示在UIImageView上並設置爲contentMode = UIViewContentModeScaleAspectFit

如果我使用,以掩蓋與UIBezierPath的UIImage的方法是好的,我最終可能[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey, nil]明確設置photoOutput設置和我也許可以使用類似how to convert a CVImageBufferRef to UIImage,改變了很多我的代碼..但我真的寧願不這樣做,除非完全有必要,因爲如我所說,我真的不知道視頻編碼/所有這些圖形,低級別的對象。

回答

1

這條線:

[originalImage drawInRect:CGRectMake(0, originalImage.size.height/-3, thumbnailSize.width, originalImage.size.height)]; 

是一個問題。您正在繪製originalImage,但您指定的寬度爲thumbnailSize.width,高度爲originalImage。這混亂了圖像的寬高比。

您需要基於相同圖像大小的寬度和高度。根據需要選擇一個以保持適當的寬高比。

+0

* facepalm * .... 只是來自處理另一個失真問題(類似於:http://stackoverflow.com/questions/9691646/why-does-my-movie-from-uiimages-gets-distorted ?rq = 1)所以我當然相信自己,這必須是我的圖像處理管道的問題...... – gadu

+0

這樣的事情讓我同時愛和恨編程(一)沒有我想象的那麼難。但是(b)太容易犯一個粗心的錯誤(比如調用錯誤的變量),把所有的東西都弄亂了 – gadu