【发布时间】:2011-10-16 15:19:06
【问题描述】:
在我的应用程序中,我使用此函数创建了一个无符号字符指针:
- (unsigned char*)getRawData
{
// First get the image into your data buffer
CGImageRef image = [self CGImage];
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), image);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
return rawData;
}
在另一个类中,我为该指针分配了一个属性,如下所示:self.bitmapData = [image getRawData];
在这个过程中,我可以在哪里释放 malloc 的内存?当我尝试释放 dealloc 中的属性时,它给了我一个 exc_bad_access 错误。我觉得我在这里缺少一个基本的 c 或 Objective-c 概念。感谢所有帮助。
【问题讨论】:
-
你如何释放财产?
-
每个分配器都有一个配对的解除分配器来释放分配的对象。查看
malloc的手册页——但最好找到与您的特定平台相关的文档——将解释应该使用什么;-) -
我尝试了 free(property) 这似乎不正确,但我还是试了一下。
标签: objective-c c pointers malloc free