代码之家  ›  专栏  ›  技术社区  ›  Daniel Soto

如何在Xcode for iOS中快速创建具有CFDataRef图像像素数据的NSMutableArray

  •  -1
  • Daniel Soto  · 技术社区  · 11 年前

    我的问题很简单,我有以下代码,它创建了一个Hues数组,该数组来自一个返回图像UIColor的函数(这并不重要,只是上下文)。所以,我需要尽快创建这个阵列,这个测试只运行一个5x5像素的图像,大约需要3秒,我想在大约2秒(顶部)内运行一个50x50像素的图像(至少),有什么想法吗?

    - (void)createArrayOfHues: (UIImage *)imageScaned{
        if (imageScaned != nil) {
    
            NSLog(@"Creating Array...");
    
            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
            img.contentMode = UIViewContentModeScaleToFill;
            img.image = imageScaned;
            img.contentMode = UIViewContentModeRedraw;
            img.hidden = YES;
    
    
    
            int i = 0;
            CGFloat hue = 0;
            CGFloat sat = 0;
            CGFloat brit = 0;
            CGFloat alph = 0;
            CGFloat hue2 = 0;
            CGFloat sat2 = 0;
            CGFloat brit2 = 0;
            CGFloat alph2 = 0;
    
            [_colorsArray removeAllObjects];
            [_satForHue removeAllObjects];
            [_britForHue removeAllObjects];
            [_alphForHue removeAllObjects];
    
            _colorsArray = [[NSMutableArray alloc] initWithCapacity:(25)];
            _satForHue = [[NSMutableArray alloc] initWithCapacity:(25)];
            _britForHue = [[NSMutableArray alloc] initWithCapacity:(25)];
            _alphForHue = [[NSMutableArray alloc] initWithCapacity:(25)];
    
            while (i<25) {
    
    
                for (int y=1; y <= 5; y++){
                    for (int x = 1; x <= 2.5; x++){
    
                        if (x != (5-x)){
                            UIColor *color = [self colorMatch:imageScaned :x :y];
                            UIColor *color2 = [self colorMatch:imageScaned :(5-x) :y];
    
    
                            if([color getHue:&hue saturation:&sat brightness:&brit alpha:&alph] && [color2 getHue:&hue2 saturation:&sat2 brightness:&brit2 alpha:&alph2]){
    
                                NSNumber *hueId = [NSNumber numberWithFloat:(float)hue];
                                NSNumber *satId = [NSNumber numberWithFloat:(float)sat];
                                NSNumber *britId = [NSNumber numberWithFloat:(float)brit];
                                NSNumber *alphId = [NSNumber numberWithFloat:(float)alph];
                                NSNumber *hueId2 = [NSNumber numberWithFloat:(float)hue2];
                                NSNumber *satId2 = [NSNumber numberWithFloat:(float)sat2];
                                NSNumber *britId2 = [NSNumber numberWithFloat:(float)brit2];
                                NSNumber *alphId2 = [NSNumber numberWithFloat:(float)alph2];
    
    
                                [_colorsArray insertObject:hueId atIndex:i];
                                [_satForHue insertObject:satId atIndex:i];
                                [_britForHue insertObject:britId atIndex:i];
                                [_alphForHue insertObject:alphId atIndex:i];
    
                                [_colorsArray insertObject:hueId2 atIndex:(i+1)];
                                [_satForHue insertObject:satId2 atIndex:(i+1)];
                                [_britForHue insertObject:britId2 atIndex:(i+1)];
                                [_alphForHue insertObject:alphId2 atIndex:(i+1)];
    
    
                            }
                            NSLog(@"color inserted at %i with x: %i and y: %i" , i , x, y);
                            i++;
                        }else {
    
                            UIColor *color = [self colorMatch:imageScaned :x :y];
                            if([color getHue:&hue saturation:&sat brightness:&brit alpha:&alph]){
                                NSNumber *hueId = [NSNumber numberWithFloat:(float)hue];
                                NSNumber *satId = [NSNumber numberWithFloat:(float)sat];
                                NSNumber *britId = [NSNumber numberWithFloat:(float)brit];
                                NSNumber *alphId = [NSNumber numberWithFloat:(float)alph];
                                [_colorsArray insertObject:hueId atIndex:i];
                                [_satForHue insertObject:satId atIndex:i];
                                [_britForHue insertObject:britId atIndex:i];
                                [_alphForHue insertObject:alphId atIndex:i];
    
                            }
    
                        }
                    }
    
                }
            }
            NSLog(@"Returns the array");
    
        }else{
            NSLog(@"Returns nothing");
        }
    }
    

    colorMatch的代码:

    - (UIColor *) colorMatch: (UIImage *)image :(int) x :(int) y {
    isBlackColored = NO;
    if (image == nil){
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        BOOL customColor = [defaults boolForKey:@"custom_color"];
    
    
    
        if (customColor){
            float red = [defaults floatForKey:@"custom_color_slider_red"];
            float green = [defaults floatForKey:@"custom_color_slider_green"];
            float blue = [defaults floatForKey:@"custom_color_slider_blue"];
            return [UIColor colorWithRed:red green:green blue:blue alpha:1];
    
        }else
            isDefaultS = YES;
    }
    else{
            CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
            const UInt8* data = CFDataGetBytePtr(pixelData);
    
            int pixelInfo = ((image.size.width  * y) + x ) * 4; 
    
            UInt8 red = data[pixelInfo];         
            UInt8 green = data[(pixelInfo + 1)]; 
            UInt8 blue = data[pixelInfo + 2];    
            UInt8 alpha = data[pixelInfo + 3];   
            CFRelease(pixelData);
            float redC = red/255.0f;
            float greenC = green/255.0f;
            float blueC = blue/255.0f;
    
            UIColor* color = [UIColor colorWithRed:redC green:greenC blue:blueC alpha:alpha/255.0f];
    
    
        return color;
    
        }
    
    return nil;
    }
    
    1 回复  |  直到 11 年前
        1
  •  2
  •   Tamás Zahola    11 年前

    我认为您的主要性能瓶颈不是NSMutableArray实例的初始化,而是索引映像的方式:

    UIColor *color = [self colorMatch:imageScaned :x :y];
    

    我想这个方法会将UIImage转换为CGImageRef,复制它的数据,对它进行索引,然后销毁/释放这些临时对象,或者类似的东西- 对于每一个像素。。。 您应该重构此代码,以便只获得一次图像缓冲区,然后像使用常规C指针/数组一样使用它。如果这不能解决您的性能问题,那么您应该进行一些评测。