In the method below, I resize an image, but I get an error while debugging that memory cannot be allocated. The image is less than 1 MB in size, so I am confused why this error pops up. Do you have any idea what causes this error to occur or do you have a strategy how I can debug this?
For your information, I have used Instruments and Guard Malloc to get to the bottom of this problem, but I haven't been able to solve this issue. I've also included the stack trace when the error (malloc_error_break) pops up.
Thanks.
- (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,
bitmapBytesPerR开发者_如何学Goow,
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)
精彩评论