图像大小调整期间的Malloc错误

时间:2011-02-20 10:59:55

标签: cocoa core-graphics

在下面的方法中,我调整了图像的大小,但在调试时无法分配内存时出错。图像的大小不到1 MB,所以我很困惑为什么弹出这个错误。您是否知道导致此错误的原因或您是否有策略如何调试此错误?

为了您的信息,我使用了Instruments和Guard Malloc来解决这个问题,但我无法解决这个问题。当错误(malloc_error_break)弹出时,我还包括了堆栈跟踪。

感谢。

- (NSData *)resizeImageWithData:(NSData *)data {
    CGFloat target = 1024.0;

    CFDataRef imageData = (CFDataRef)data;

    CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData(imageData);
    CGImageRef imageRef = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, YES, kCGRenderingIntentDefault);

    CGFloat imageWidth = (CGFloat)CGImageGetWidth(imageRef);
    CGFloat imageHeight = (CGFloat)CGImageGetHeight(imageRef);

    CGFloat widthFactor = imageWidth / target;
    CGFloat heightFactor = imageHeight / target;
    CGFloat factor = 1.0;

    if (widthFactor < 1.0 && heightFactor < 1.0) {
        factor = 1.0;
    } else {
        if (widthFactor >= heightFactor) {
            factor = widthFactor;
        } else {
            factor = heightFactor;
        }
    }

    unsigned newWidth = floorf(imageWidth / factor);
    unsigned newHeight = floorf(imageHeight / factor);

    int bitmapBitsPerComponent = CGImageGetBitsPerComponent(imageRef);
    int bitmapBytesPerRow = 4 * newWidth;
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
    CGBitmapInfo bitmapData = CGImageGetBitmapInfo(imageRef);

    **CGContextRef context = CGBitmapContextCreate(NULL,
                                                 newWidth,
                                                 newHeight,
                                                 bitmapBitsPerComponent,
                                                 bitmapBytesPerRow,
                                                 colorSpace,
                                                 bitmapData);**

    CGContextDrawImage(context, CGRectMake(0, 0, newWidth, newHeight), imageRef);
    CGImageRef newImage = CGBitmapContextCreateImage(context);
    CFMutableDataRef mutableData = (CFMutableDataRef)[[NSMutableData alloc] init];
    CGImageDestinationRef destination = CGImageDestinationCreateWithData(mutableData, kUTTypeJPEG, 1, NULL);
    CGImageDestinationAddImage(destination, newImage, NULL);

    if ( !(CGImageDestinationFinalize(destination)) ) {
        NSLog(@"Error while writing image data and properties.");
    }

    NSData *result = [NSData dataWithData:(NSData *)mutableData];

    CGDataProviderRelease(imageDataProvider);
    CGImageRelease(imageRef);
    CGImageRelease(newImage);

    if (destination != NULL) {
        CFRelease(destination);
    }

    CGContextRelease(context);

    [(NSMutableData *)mutableData release];

    return result;
}

#0  0x93472f82 in malloc_error_break
#1  0x9347412c in szone_error
#2  0x9337eec0 in allocate_pages
#3  0x9338f7f2 in large_malloc
#4  0x93381042 in szone_malloc_should_clear
#5  0x00040114 in GMmalloc_zone_malloc
#6  0x99864bb0 in ImageIO_Malloc
#7  0x99878bb6 in copyImageBlockSetJPEG
#8  0x99863191 in ImageProviderCopyImageBlockSetCallback
#9  0x9531464b in CGImageProviderCopyImageBlockSet
#10 0x95318e5c in img_blocks_create
#11 0x95318ce1 in img_blocks_extent
#12 0x9537d7a9 in img_interpolate_extent
#13 0x952b1b76 in img_data_lock
#14 0x952aec72 in CGSImageDataLock
#15 0x991b8750 in ripc_AcquireImage
#16 0x991b63c6 in ripc_DrawImage
#17 0x952ae8ec in CGContextDrawImage
#18 0x0000dea7 in -[ViewController resizeImageWithData:] at ViewController.m:2052

Process:         MyApplication [23370]
Path:            /Users/Pixie/MyApplication/build/Debug/MyApplication.app/Contents/MacOS/MyApplication
Identifier:      com.pixie.MyApplication
Version:         1.1.1 (1.1.1)
Code Type:       X86 (Native)
Parent Process:  ??? [1]

Date/Time:       2011-02-19 23:57:31.779 +0100
OS Version:      Mac OS X 10.6.6 (10J567)
Report Version:  6

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000
Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Thread 0 Crashed:  Dispatch queue: com.apple.main-thread
0   libSystem.B.dylib               0x913eaf82 malloc_error_break + 6
1   libSystem.B.dylib               0x913ec12c szone_error + 287
2   libSystem.B.dylib               0x912f6ec0 allocate_pages + 243
3   libSystem.B.dylib               0x913077f2 large_malloc + 1093
4   libSystem.B.dylib               0x912f9042 szone_malloc_should_clear + 3656
5   libgmalloc.dylib                0x00040114 GMmalloc_zone_malloc + 71
6   com.apple.ImageIO.framework     0x933ddbb0 ImageIO_Malloc + 26
7   com.apple.ImageIO.framework     0x933f1bb6 copyImageBlockSetJPEG + 2600
8   com.apple.ImageIO.framework     0x933dc191 ImageProviderCopyImageBlockSetCallback + 174
9   com.apple.CoreGraphics          0x9163864b CGImageProviderCopyImageBlockSet + 228
10  com.apple.CoreGraphics          0x9163ce5c img_blocks_create + 348
11  com.apple.CoreGraphics          0x9163cce1 img_blocks_extent + 85
12  com.apple.CoreGraphics          0x916a17a9 img_interpolate_extent + 163
13  com.apple.CoreGraphics          0x915d5b76 img_data_lock + 8882
14  com.apple.CoreGraphics          0x915d2c72 CGSImageDataLock + 172
15  libRIP.A.dylib                  0x94963750 ripc_AcquireImage + 2446
16  libRIP.A.dylib                  0x949613c6 ripc_DrawImage + 1245
17  com.apple.CoreGraphics          0x915d28ec CGContextDrawImage + 450
18  com.pixie.MyApplication         0x0000dfbb -[ViewController resizeImageWithData:] + 1153 (ViewController.m:2049)
19  com.pixie.MyApplication         0x0000d9d7 -[ViewController getImageDataForPath:] + 247 (ViewController.m:1945)
20  com.pixie.MyApplication         0x00006622 -[ViewController handleReceivedData:] + 9258 (ViewController.m:585)
21  com.pixie.MyApplication         0x00008627 -[ViewController connection:receivedData:] + 49 (ViewController.m:1015)

0 个答案:

没有答案