-1

在编写我的字典以创建 CG 缩略图时,如果我将值直接映射到 int 它完美地工作:

 NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
                       (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                       (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                       (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                       [NSNumber numberWithInt:56], kCGImageSourceThumbnailMaxPixelSize,
                       nil];
.
.
.
.
.
CGImageRef imref = CGImageSourceCreateThumbnailAtIndex(src, 0, (CFDictionaryRef)d);

现在我希望能够将该值 (56) 更改为可以在界面中更改的属性,因此我在标题中创建了一个 int

my.h
int thumbSize;

//within my init method Iinitialize it:
        thumbSize = 56;
//then I change the reference in the dictionary:

    NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
                       (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                       (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                       (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                       [NSNumber numberWithInt:thumbSize], kCGImageSourceThumbnailMaxPixelSize,
                       nil];

如果我这样做,它会崩溃并出现以下错误:

3 月 25 日 11:39:49 Charles-Romestants-MacBook.local PhotoMosaic[3583] : ImageIO: CGImageSourceCreateWithData 数据参数为零
3 月 25 日 11:39:49 Charles-Romestants-MacBook.local PhotoMosaic[3583] : ImageIO: CGImageSourceCreateImageAtIndex 图像源参数为 nil
2013-03-25 11:39:49.197 PhotoMosaic[3583:707] *** -[__NSArrayM insertObject:atIndex:]:对象不能为 nil
2013-03-25 11:39:49.201 PhotoMosaic[3583:707] (
    0 核心基础 0x00007fff912ccb06 __exceptionPreprocess + 198
    1 libobjc.A.dylib 0x00007fff8bc833f0 objc_exception_throw + 43
    2 核心基础 0x00007fff9127d27a -[__NSArrayM insertObject:atIndex:] + 282
    3 PhotoMosaic 0x00000001000023bf -[BlendingView setMosaicImages:] + 1071
    4 PhotoMosaic 0x0000000100003034 -[控制器创建马赛克:] + 84
    5 AppKit 0x00007fff8ec3d989-[NSApplication sendAction:to:from:] + 342
    6 AppKit 0x00007fff8ec3d7e7-[NSControl sendAction:to:] + 85
    7 AppKit 0x00007fff8ec3d71b-[NSCell_sendActionFrom:] + 138
    8 AppKit 0x00007fff8ec3bc03-[NSCell tr​​ackMouse:inRect:ofView:untilMouseUp:] + 1855
    9 AppKit 0x00007fff8ec3b451-[NSButtonCell tr​​ackMouse:inRect:ofView:untilMouseUp:] + 504
    10 AppKit 0x00007fff8ec3abcc -[NSControl mouseDown:] + 820
    11 AppKit 0x00007fff8ec3253e-[NSWindow 发送事件:] + 6853
    12 AppKit 0x00007fff8ec2e674-[NSApplication sendEvent:] + 5761
    13 AppKit 0x00007fff8eb4424a -[NSApplication 运行] + 636
    14 AppKit 0x00007fff8eae8c06 NSApplicationMain + 869
    15 PhotoMosaic 0x0000000100001d52 主 + 34
    16 PhotoMosaic 0x0000000100001d24 开始 + 52
)

为什么会这样,我正在传递 NSNumber +numberWithInt 一个 int ......

[编辑] 添加整个方法:

-(void)setMosaicImages:(NSMutableArray *)_mosaicImages
{
    NSLog(@"Entering here in setMosaicImages, we have %lu images",_mosaicImages.count);
    mosaicImages = _mosaicImages;
    CGImageSourceRef source;

    source = CGImageSourceCreateWithData((CFDataRef)[canvasImage TIFFRepresentation], NULL);
    CGImageRef image =  CGImageSourceCreateImageAtIndex(source, 0, NULL);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(NULL,
                                             CGImageGetWidth(image), CGImageGetHeight(image),
                                             8, CGImageGetWidth(image) * 4,
                                             colorspace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colorspace);

    NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
                       (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                       (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                       (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                       [NSNumber numberWithInt:thumbSize], kCGImageSourceThumbnailMaxPixelSize,
                       nil];

//thumbSize is declared in.h and initialized in init method with a value of 56 (int thumbSize; then thumbSize = 56; if I change the parameter to literal 56 it works.    
//create NSMUTABLE array with thumbnails for each mosaic image
    mosaicCGThumbs = [[NSMutableArray alloc]initWithCapacity:mosaicCGThumbs.count];
    NSURL* url;
    for(int i=0; i<mosaicImages.count;i++)
    {
        url = [NSURL fileURLWithPath:[(myImageObject *)[mosaicImages objectAtIndex:i] getPath]];
        CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
        CGImageRef imref = CGImageSourceCreateThumbnailAtIndex(src, 0, (CFDictionaryRef)d);
        [mosaicCGThumbs addObject:imref];
        CGImageRelease(imref);

    }
    CGRect r;
    CGImageRef thumbRef;
    int x_offset=0;
    int y_offset = 0;
    while(y_offset < CGImageGetHeight(image))
    {
        while (x_offset<CGImageGetWidth(image))
        {
            thumbRef = (CGImageRef)[mosaicCGThumbs objectAtIndex:arc4random() % mosaicCGThumbs.count];
            r = CGRectMake(x_offset, y_offset, CGImageGetWidth(thumbRef), CGImageGetHeight(thumbRef));
            CGContextSetBlendMode(ctx, kCGBlendModeCopy);
            CGContextDrawImage(ctx, r, thumbRef);

            x_offset+=CGImageGetWidth(thumbRef);
        }
        x_offset=0;
        y_offset+=56;
    }
    NSLog(@"Finished Drawing Mosaic BG");

    image = CGBitmapContextCreateImage(ctx);
    //
    //    CIImage* newImage = [[[CIImage alloc] initWithCGImage:image] autorelease];
    //    CGImageRelease(image);
    CGContextRelease(ctx);
    canvasImage = [[NSImage alloc] initWithCGImage:image size:NSZeroSize];
    mosaicPresent=YES;
    [self setNeedsDisplay:YES];

  }

为调试目的添加更多信息:

我决定看看创建的 NSDictionary 有什么不同,所以

我添加了以下代码和一个要检查的断点:

    NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
                   (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                   [NSNumber numberWithInt:thumbSize], kCGImageSourceThumbnailMaxPixelSize,
                   nil];

NSDictionary* d2 = [NSDictionary dictionaryWithObjectsAndKeys:
                   (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                   [NSNumber numberWithInt:56], kCGImageSourceThumbnailMaxPixelSize,
                   nil];

用调试器查看它,我得到了这个:(看看两者在缩略图像素大小的值上有何不同。将对此进行更多研究。

(gdb) po d
{
    kCGImageSourceCreateThumbnailFromImageAlways = 1;
    kCGImageSourceCreateThumbnailWithTransform = 1;
    kCGImageSourceShouldAllowFloat = 1;
    kCGImageSourceThumbnailMaxPixelSize = 0;
}
(gdb) po d2
{
    kCGImageSourceCreateThumbnailFromImageAlways = 1;
    kCGImageSourceCreateThumbnailWithTransform = 1;
    kCGImageSourceShouldAllowFloat = 1;
    kCGImageSourceThumbnailMaxPixelSize = 56;
}
4

2 回答 2

1

您确定thumbSize执行此代码时是 56 吗?似乎不太可能。

我在你的 pastebin 中看到你有一个-init和一个-initWithFrame:方法。如果你用 初始化你的对象-initWithFrame:,那么你的-init方法可能没有被调用,因此没有设置thumbSize. 这是一个奇怪的怪癖(IMO;其他人可能不同意),标准实现-initWithFrame:不调用-init.

于 2013-03-26T23:48:45.857 回答
0

好的,感谢您的帮助:错误比预期的还要简单,没有调用 init 方法来初始化变量,我不得不将它放在 initWithFrame 中:

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        mosaicPresent = NO;
    }

    return self;
}
- (id)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    invalidateCanvas = YES;
    thumbSize = 56;

    return self;
}
于 2013-03-26T23:52:10.877 回答