用于获取逻辑中像素的RGB值的新iPad非常慢

时间:2012-08-20 05:29:17

标签: objective-c xcode ipad opengl-es

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;
}

1 个答案:

答案 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;
}