カメラ画像(または写真集からの画像)へのアクセスにいくつかの問題があります。サイズ変更された画像の生のピクセルデータにアクセスする際のエラー
UIImageのサイズを変更した後(私はいくつかの異なるサイズ変更メソッドをテストしましたが、それらはすべて同じエラーに繋がります)複雑なアルゴリズムに渡すために個々のピクセルにアクセスしたいと思います。
問題は、CGImageGetDataProvider - > EXC_BAD_ACCESSエラーが発生する生のピクセルデータにアクセスするときに、画像サイズ(たとえば、幅* 4)に一致しないbytesPerRow値がしばしばあることです。
たぶん、私たちはここに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
作業用としてpng表現に変換する使用 – faroit