0

NewiPad to get RGB values ​​of pixels in the logic is very slow but, iPad1, iPad2 is very fast.. Why?

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

1 回答 1

1

只需进行代码优化:

如果此函数仅适用于“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;
}
于 2012-08-20T08:37:15.387 回答