在编写我的字典以创建 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 trackMouse:inRect:ofView:untilMouseUp:] + 1855 9 AppKit 0x00007fff8ec3b451-[NSButtonCell trackMouse: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; }