在编写我的词典以创建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];
如果我这样做,它会因以下错误崩溃:
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];
使用调试器查看它我得到了这个:(看看两者在拇指尾的像素大小的值方面有何不同。将会更多地考虑这一点。
(gdb) po d { kCGImageSourceCreateThumbnailFromImageAlways = 1; kCGImageSourceCreateThumbnailWithTransform = 1; kCGImageSourceShouldAllowFloat = 1; kCGImageSourceThumbnailMaxPixelSize = 0; } (gdb) po d2 { kCGImageSourceCreateThumbnailFromImageAlways = 1; kCGImageSourceCreateThumbnailWithTransform = 1; kCGImageSourceShouldAllowFloat = 1; kCGImageSourceThumbnailMaxPixelSize = 56; }
答案 0 :(得分:1)
执行此代码时,您确定thumbSize
是56吗?这似乎不太可能。
我在您的pastebin中看到您同时拥有-init
和-initWithFrame:
方法。如果您使用-initWithFrame:
初始化对象,则可能未调用-init
方法,因此未设置thumbSize
的值。这是一个奇怪的怪癖(IMO;其他人可能不同意)-initWithFrame:
的标准实现不会调用-init
。
答案 1 :(得分: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;
}