当我使用dispatch_apply将一些数据添加到nscountset时,我在模拟器中崩溃,但在iPhone中崩溃了。并得到一条消息“-[__ NSArrayI isEqual:]:消息发送到已释放实例0x60000024c5a0”,我找不到它的发生情况。

CGSize thumbSize = CGSizeMake(200,200);
NSCountedSet *cls2 = [NSCountedSet setWithCapacity:thumbSize.width * thumbSize.height];
dispatch_apply(thumbSize.width, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
    int x = (int)index;
    for (int y = 0; y < thumbSize.height; y++) {
        if(y<x){
            continue;
        }
        int offset = 4*(x*y);
        int red = data[offset];
        int green = data[offset+1];
        int blue = data[offset+2];
        int alpha = data[offset+3];
        NSArray *clr2 = @[@(red),@(green),@(blue),@(alpha)];
        [cls2 addObject:clr2];
    }

});

[cls2 addobject:clr2]中崩溃;日志为“-[__ NSArrayI isEqual:]:消息发送到已释放实例0x60000024c5a0”。
而且我的功能中有所有代码,我想在图像中获得最多的颜色。
CGSize thumbSize = CGSizeMake(200, 200);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(NULL, thumbSize.width, thumbSize.height, 8, thumbSize.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGRect drawRect = CGRectMake(0, 0, thumbSize.width, thumbSize.height);

CGContextDrawImage(context, drawRect, self.CGImage);

CGColorSpaceRelease(colorSpace);
unsigned char *data = CGBitmapContextGetData(context);
if (data == NULL)
{
    return nil;
}
NSCountedSet *cls2 = [NSCountedSet setWithCapacity:thumbSize.width * thumbSize.height];
dispatch_apply(thumbSize.width, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
    int x = (int) index;
    for (int y = 0; y < thumbSize.height; y++)
    {

        if (y < x)
        {
            continue;
        }
        int offset = 4 * (x * y);
        int red = data[offset];
        int green = data[offset + 1];
        int blue = data[offset + 2];
        int alpha = data[offset + 3];
        NSArray *clr3 = @[@(red), @(green), @(blue), @(alpha)];
        [cls2 addObject:clr3];
    }

});

CGContextRelease(context);

NSEnumerator *enumerator = [cls2 objectEnumerator];
NSArray *curColor = nil;
NSArray *maxColor = nil;
NSUInteger maxCount = 0;
while ((curColor = [enumerator nextObject]) != nil)
{
    NSUInteger tmpCount = [cls2 countForObject:curColor];
    if (tmpCount < maxCount) {
        continue;
    }

    maxCount = tmpCount;
    maxColor = curColor;
}

NSLog(@"colors: RGB A %f %d %d  %d",[maxColor[0] floatValue],[maxColor[1] intValue],[maxColor[2] intValue],[maxColor[3] intValue]);

return [UIColor colorWithRed:([maxColor[0] intValue]/255.0f) green:([maxColor[1] intValue]/255.0f) blue:([maxColor[2] intValue]/255.0f) alpha:([maxColor[3] intValue]/255.0f)];

最佳答案

NSCountedSet不是线程安全的-reference。正如您所发现的那样,尝试从并发线程中更改一个将导致无法预测的结果。

使用没有for的简单dispatch_apply循环(如果需要,可以在后台线程上调度)来处理数据,或者通过在串行调度队列上调度NSCountedSet更新来保护它。

关于ios - 当我使用dispatch_apply时在模拟器中崩溃但iPhone,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43861281/

10-13 05:02