我有一种方法可以调整 CGImageRef
的大小并返回 CGImageRef
。问题是最后几行,我需要以某种方式释放但之后返回它。有任何想法吗?谢谢
-(CGImageRef)resizeImage:(CGImageRef *)anImage width:(CGFloat)width height:(CGFloat)height
{
CGImageRef imageRef = *anImage;
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
CGContextRelease(bitmap);
CGImageRelease(ref); //issue here
return ref;
}
最佳答案
Cocoa 内存管理命名策略规定,您拥有一个对象,该对象是从名称以 alloc、copy 或 new.
Clang 静态分析器也遵守此规则。
请注意,Core Foundation 的约定略有不同。详情可见Apple's Advanced Memory Management Programming Guide .
我修改了您的上述方法以符合该命名约定。我在传入 anImage
时也删除了星号,因为 CGImageRef
已经是一个指针。 (或者这是故意的?)
请注意,您拥有返回的 CGImage
并且稍后必须 CGImageRelease
它。
-(CGImageRef)newResizedImageWithImage:(CGImageRef)anImage width:(CGFloat)width height:(CGFloat)height
{
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(anImage);
if (alphaInfo == kCGImageAlphaNone)
{
alphaInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(anImage), 4 * width, CGImageGetColorSpace(anImage), alphaInfo);
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), anImage);
CGImageRef image = CGBitmapContextCreateImage(bitmap);
CGContextRelease(bitmap);
return image;
}
关于ios - 如果需要返回它,如何释放 CGImageRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17016247/