我初始化像這樣存儲的UIImage像素數據轉換成C數組,無法確定數組的元素計數
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, bounds);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
然而,當我試圖檢查通過的NSLog計數陣列,總是得到4(4/1 ,具體而言)。
int count = sizeof(rawData)/sizeof(rawData[0]);
NSLog(@"%d", count);
但是,當我NSLog單個元素的值,它返回非零值。
ex。
CGFloat f1 = rawData[15];
CGFloat f2 = rawData[n]
,其中n
是image width*height*4
;
//我沒想到這個工作,因爲最後一個元素應該是N-1
最後,我想
int n = lipBorder.size.width *lipBorder.size.height*4*2; //lipBorder holds the image's dimensions, I tried multiplying by 2 because there are 2 pixels for every CGPoint in retina
CGFloat f = rawData[n];
這將每次返回不同的值相同的圖像, (例如0.000,115.000,38.000)。
如何確定計數/數值如何存儲到數組中?
好的,但爲什麼我得到寬*高* 4的值? – Mahir
@馬希爾:我不確定我是否遵守。用'n'的參數調用'malloc'就意味着「給我分配內存的n個字節」。如果你從'malloc'得到一個有效的指針,你可以放心,它指向一塊(至少)'n'字節的內存。在你的情況下,你可以使用'height * width * 4'的'n'來調用'malloc'並獲得一個有效的指針,所以你知道'rawData'的長度必須是'height * width * 4'字節。無需任何其他間接手段來確定其大小。 – Mac
對不起,我認爲參數是元素的數量,而不是內存大小。但是,如果每個元素都是1個字節,它們會不會像你所說的一樣嗎? – Mahir