2013-03-25 23 views
-1

以書面形式我的字典了CG生成縮略圖,如果我的價值直接映射到一個int它完美:爲目標C字典問題(的NSNumber + NumberWithInt)

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

如果我這樣做,它有以下錯誤崩潰:

 
Mar 25 11:39:49 Charles-Romestants-MacBook.local PhotoMosaic[3583] : ImageIO: CGImageSourceCreateWithData data parameter is nil 
Mar 25 11:39:49 Charles-Romestants-MacBook.local PhotoMosaic[3583] : ImageIO: CGImageSourceCreateImageAtIndex image source parameter is nil 
2013-03-25 11:39:49.197 PhotoMosaic[3583:707] *** -[__NSArrayM insertObject:atIndex:]: object cannot be nil 
2013-03-25 11:39:49.201 PhotoMosaic[3583:707] (
    0 CoreFoundation      0x00007fff912ccb06 __exceptionPreprocess + 198 
    1 libobjc.A.dylib      0x00007fff8bc833f0 objc_exception_throw + 43 
    2 CoreFoundation      0x00007fff9127d27a -[__NSArrayM insertObject:atIndex:] + 282 
    3 PhotoMosaic       0x00000001000023bf -[BlendingView setMosaicImages:] + 1071 
    4 PhotoMosaic       0x0000000100003034 -[Controller createMosaic:] + 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 trackMouse:inRect:ofView:untilMouseUp:] + 1855 
    9 AppKit        0x00007fff8ec3b451 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 504 
    10 AppKit        0x00007fff8ec3abcc -[NSControl mouseDown:] + 820 
    11 AppKit        0x00007fff8ec3253e -[NSWindow sendEvent:] + 6853 
    12 AppKit        0x00007fff8ec2e674 -[NSApplication sendEvent:] + 5761 
    13 AppKit        0x00007fff8eb4424a -[NSApplication run] + 636 
    14 AppKit        0x00007fff8eae8c06 NSApplicationMain + 869 
    15 PhotoMosaic       0x0000000100001d52 main + 34 
    16 PhotoMosaic       0x0000000100001d24 start + 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]; 

用調試器查看它我得到了這個:(看看這兩個值在thumbneail的像素大小的差異。將更多地考慮這一點。

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

怎麼樣使用'@(52)'放置'[NSNumber numberWithInt:thumbSize]'? – 2013-03-25 16:55:25

+6

你得到的錯誤看起來並不明確地來自你發佈的代碼,因爲異常實際上是在對數組方法的調用中發生的。你能爲我們發佈「BlendingView -setMosaicImages:」的整個文本嗎? – 2013-03-25 16:56:34

+2

錯誤是說_source_參數爲零,與字典無關。 – 2013-03-25 16:57:17

回答

1

你確定thumbSize是56這個代碼執行時?這似乎不太可能。

我在你的pastebin中看到你有一個-init-initWithFrame:方法。如果使用-initWithFrame:初始化對象,那麼您的-init方法可能未被調用,因此不會設置thumbSize的值。這是一個奇怪的怪癖(國際海事組織;其他人可能不同意)-initWithFrame:的標準實施不叫-init

+0

這就是它,只是在調試器幫助下發現。因爲你的答案已經發布,所以我的回答並不在這裏,而是標記爲答案。 – cromestant 2013-03-26 23:53:05

+0

很高興您能以某種方式解決問題。 – 2013-03-26 23:53:39

0

好的,謝謝你的幫助: 錯誤是更簡單的比預期中,init方法notbeing稱爲啓動變量,我不得不把它放在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; 
}