NewiPad在逻辑中获取像素的RGB值非常慢 但是,iPad1,iPad2非常快...... 为什么呢?
- (RGB) getTouchAreaPixelRGB: (CGPoint) point
{
NSString * str_BackImageName = @ "butterfly_BG.png";
CGImageRef cgBackImage = CGImageRetain ([UIImage imageNamed: str_BackImageName]. CGImage);
size_t width = CGImageGetWidth (cgBackImage);
size_t height = CGImageGetHeight (cgBackImage);
CGDataProviderRef backProvider = CGImageGetDataProvider (cgBackImage);
CFDataRef backBitmapData = CGDataProviderCopyData (backProvider);
const UInt8 * backData CFDataGetBytePtr (backBitmapData);
RGB rgb;
if ((point.x <width) && (point.y <height))
{
size_t offset = ((width * point.y) + point.x) * 4;
rgb.nRed = backData [offset];
rgb.nGreen = backData [offset +1];
rgb.nBlue = backData [offset +2];
}
return rgb;
}
答案 0 :(得分:1)
只需进行代码优化:
如果此func仅适用于“butterfly_BG.png”,则不要调用
NSString * str_BackImageName = @ "butterfly_BG.png";
CGImageRef cgBackImage = CGImageRetain ([UIImage imageNamed: str_BackImageName]. CGImage);
size_t width = CGImageGetWidth (cgBackImage);
size_t height = CGImageGetHeight (cgBackImage);
CGDataProviderRef backProvider = CGImageGetDataProvider (cgBackImage);
CFDataRef backBitmapData = CGDataProviderCopyData (backProvider);
const UInt8 * backData CFDataGetBytePtr (backBitmapData);
每次。在应用程序启动时,只需一次调用它。然后你的新功能将如何:
- (RGB) getTouchAreaPixelRGB: (CGPoint) point
{
RGB rgb;
if ((point.x <width) && (point.y <height))
{
size_t offset = ((width * point.y) + point.x) * 4;
rgb.nRed = backData [offset];
rgb.nGreen = backData [offset +1];
rgb.nBlue = backData [offset +2];
}
return rgb;
}