我试图理解为什么每次我在关卡加载路由中调用以下代码块时,它都会根据 XCode 的 5.0.1 内存监视器泄漏内存。此代码块旨在根据其 alpha 通道检查图像的实际边界。
UIImageView* iv = (UIImageView*)m_image->GetId();
// First get the image into your data buffer
CGImageRef image = [iv.image CGImage];
int width = CGImageGetWidth(image);
int height = CGImageGetHeight(image);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void* rawData = malloc(height * width * 4);
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
int bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGContextRelease(context);
// rawData contains the image data in the RGBA8888 pixel format.
int lx=99999999,bx=0,ly=99999999,by=0;
for( int x=0; x<width; x++ )
{
for( int y=0; y<height; y++ )
{
int pixelInfo = (bytesPerRow * y) + x * bytesPerPixel; // data is a png
unsigned char* d = (unsigned char*)rawData;
if( d[pixelInfo + 3] ) // check alpha
{
if( x < lx ) lx = x;
if( x > bx ) bx = x;
if( y < ly ) ly = y;
if( y > by ) by = y;
}
}
}
free(rawData);
如果你们有更好的方法来解决这个问题,那么请提出建议,但是这个实现很有效,因为它会泄漏。
谢谢。