我在訪問相機圖像(甚至是相冊中的圖像)時遇到了一些問題。我調整了UIImage的大小(我測試了幾個不同的調整大小的方法,它們都導致相同的錯誤),我想訪問每個像素以交給一個複雜的算法。訪問已調整大小的圖像的原始像素數據時出錯
問題是,當使用CGImageGetDataProvider訪問原始像素數據時,經常存在與圖像大小(例如寬度* 4)不匹配的bytesPerRow值 - >導致出現EXC_BAD_ACCESS錯誤。
也許我們有一個iOS的錯誤在這裏...
儘管如此,這裏是代碼:
// UIImage capturedImage from Camera
CGImageRef capturedImageRef = capturedImage.CGImage;
// getting bits per component from capturedImage
size_t bitsPerComponentOfCapturedImage = CGImageGetBitsPerComponent(capturedImageRef);
CGImageAlphaInfo alphaInfoOfCapturedImage = CGImageGetAlphaInfo(capturedImageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// calculate new size from interface data.
// with respect to aspect ratio
// ...
// newWidth = XYZ;
// newHeight = XYZ;
CGContextRef context = CGBitmapContextCreate(NULL, newWidth, newHeight, bitsPerComponentOfCapturedImage,0 , colorSpace, alphaInfoOfCapturedImage);
// I also tried to make use getBytesPerRow for CGBitmapContextCreate resulting in the same error
// if image was rotated
if(capturedImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -newHeight, 0.0f);
}
// draw on new context with new size
CGContextDrawImage(context, CGRectMake(0, 0, newWidth, newHeight), capturedImage.CGImage);
CGImageRef scaledImage=CGBitmapContextCreateImage(context);
// release
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
theImage = [UIImage imageWithCGImage: scaledImage];
CGImageRelease(scaledImage);
在那之後,我想
CGImageRef imageRef = theImage.CGImage;
NSData *data = (NSData *) CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
unsigned char *pixels = (unsigned char *)[data bytes];
// create a new image from the modified pixel data
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, [data length], NULL);
NSLog(@"bytesPerRow: %f ", (float)bytesPerRow);
NSLog(@"Image width: %f ", (float)width);
NSLog(@"Image height: %f ", (float)height);
// manipulate the individual pixels
for(int i = 0; i < [data length]; i += 4) {
// accessing (float) pixels[i];
// accessing (float) pixels[i+1];
// accessing (float) pixels[i+2];
}
所以對於訪問縮放圖像例如,當我訪問511x768像素的圖像和縮小到290x436的縮放比例時,我得到以下輸出:
圖像寬度: 290.000000
圖像高度: 436.000000
bitsPerComponent: 8.000000
bitsPerPixel: 32.000000
bytesPerRow: 1184.000000
並且您可以清楚地看到bytesPerRow(儘管可可選擇自動選擇)而不是與圖像寬度匹配。
我很想看到任何幫助
上的Xcode 4使用的是iOS SDK 4.3
使用轉換爲pngrepresentation作爲工作周圍 – faroit