我尝试将图片的所有像素值添加到数组中。如果图片太大我的应用程序失去了与资产的连接并崩溃..
如何查看内存是否正在增长以停止并显示警报,因为我不知道在加载图片时可以采取的最大图像大小是什么来防止这种情况。
我的代码是:
-(NSArray*)getRGBAFromImage:(UIImage*)image atx:(int)xp atY:(int)yp
{
NSMutableArray *resultColor = [NSMutableArray array];
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yp) + xp * bytesPerPixel;
//EDIT: THIS IS THE LOOP
for (int i = 0 ; i < count ; ++i)
{
CGFloat red = (rawData[byteIndex] *1) ;
CGFloat green = (rawData[byteIndex + 1] *1) ;
CGFloat blue = (rawData[byteIndex + 2] *1) ;
CGFloat alpha = (rawData[byteIndex + 3] *1) ;
byteIndex += bytesPerPixel;
redTotal = redTotal+red;
greenTotal = greenTotal + green;
blueTotal = blueTotal + blue;
UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
NSLog(@"width:%i hight:%i Color:%@",width,height,[color description]);
free(rawData);
return resultColor;
}
或者我应该将它添加到队列中吗?
谢谢!
答案 0 :(得分:0)
Wile图像相当大(32MB),因为它是一个对象数组,所以数组会更大。将有8M UIColor颜色对象。只有64位设备中指向UIColor对象的指针数组才需要64MB,然后加上UIColor对象大小的8M倍。
您需要找到另一种处理方法。