gpt4 book ai didi

ios - iOS 上的 CGImage 旋转方法内存泄漏

转载 作者:行者123 更新时间:2023-12-02 15:55:56 25 4
gpt4 key购买 nike

我有这个代码来旋转 CGImage:

- (CGImageRef)rotateCGImageRef:(CGImageRef)imageRef toOrientation:(UIImageOrientation)orientation
{
CGRect bnds = CGRectZero;
CGImageRef copy = nil;
CGContextRef ctxt = nil;
CGRect rect = CGRectZero;
CGAffineTransform tran = CGAffineTransformIdentity;

@autoreleasepool {
bnds.size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); //self.size;
rect.size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); //self.size;

switch (orientation)
{
case UIImageOrientationUp:
return imageRef;

case UIImageOrientationUpMirrored:
tran = CGAffineTransformMakeTranslation(rect.size.width, 0.0);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
break;

case UIImageOrientationDown:
tran = CGAffineTransformMakeTranslation(rect.size.width, rect.size.height);
tran = CGAffineTransformRotate(tran, degreesToRadians(180.0));
break;

case UIImageOrientationDownMirrored:
tran = CGAffineTransformMakeTranslation(0.0, rect.size.height);
tran = CGAffineTransformScale(tran, 1.0, -1.0);
break;

case UIImageOrientationLeft:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(0.0, rect.size.width);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;

case UIImageOrientationLeftMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height, rect.size.width);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;

case UIImageOrientationRight:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height, 0.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;

case UIImageOrientationRightMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeScale(-1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;

default:
// orientation value supplied is invalid
assert(false);
return nil;
}

UIGraphicsBeginImageContext(bnds.size);
ctxt = UIGraphicsGetCurrentContext();

switch (orientation)
{
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextScaleCTM(ctxt, -1.0, 1.0);
CGContextTranslateCTM(ctxt, -rect.size.height, 0.0);
break;

default:
CGContextScaleCTM(ctxt, 1.0, -1.0);
CGContextTranslateCTM(ctxt, 0.0, -rect.size.height);
break;
}

CGContextConcatCTM(ctxt, tran);
CGContextDrawImage(ctxt, rect, imageRef);

//copy = UIGraphicsGetImageFromCurrentImageContext();
copy = CGBitmapContextCreateImage(ctxt);
UIGraphicsEndImageContext();
}
return copy;
}

但是分析器说它有泄漏: memory leak

我无法释放复制对象,因为我的方法正在返回它。

到底泄漏了什么?

编辑:下面是转换为 C 函数的最终代码:

CGImageRef CreateRotatedImage(CGImageRef imageRef, UIImageOrientation orientation)
{
CGRect bnds = CGRectZero;
CGImageRef copy = nil;
CGContextRef ctxt = nil;
CGRect rect = CGRectZero;
CGAffineTransform tran = CGAffineTransformIdentity;

bnds.size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); //self.size;
rect.size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); //self.size;

switch (orientation)
{
case UIImageOrientationUp:
return imageRef;

case UIImageOrientationUpMirrored:
tran = CGAffineTransformMakeTranslation(rect.size.width, 0.0);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
break;

case UIImageOrientationDown:
tran = CGAffineTransformMakeTranslation(rect.size.width, rect.size.height);
tran = CGAffineTransformRotate(tran, degreesToRadians(180.0));
break;

case UIImageOrientationDownMirrored:
tran = CGAffineTransformMakeTranslation(0.0, rect.size.height);
tran = CGAffineTransformScale(tran, 1.0, -1.0);
break;

case UIImageOrientationLeft:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(0.0, rect.size.width);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;

case UIImageOrientationLeftMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height, rect.size.width);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;

case UIImageOrientationRight:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height, 0.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;

case UIImageOrientationRightMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeScale(-1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;

default:
// orientation value supplied is invalid
assert(false);
return nil;
}

UIGraphicsBeginImageContext(bnds.size);
ctxt = UIGraphicsGetCurrentContext();

switch (orientation)
{
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextScaleCTM(ctxt, -1.0, 1.0);
CGContextTranslateCTM(ctxt, -rect.size.height, 0.0);
break;

default:
CGContextScaleCTM(ctxt, 1.0, -1.0);
CGContextTranslateCTM(ctxt, 0.0, -rect.size.height);
break;
}

CGContextConcatCTM(ctxt, tran);
CGContextDrawImage(ctxt, rect, imageRef);

//copy = UIGraphicsGetImageFromCurrentImageContext();
copy = CGBitmapContextCreateImage(ctxt);
UIGraphicsEndImageContext();
return copy;
}

调用此 C 函数:

imageRef = CreateRotatedImage(imageRef, UIImageOrientationRight);

最佳答案

我认为你的问题是你从 Obj-C 方法返回 CGImageRefCGImageRef 是 C 结构指针的 typedef,而不是 NSObject,因此它不能自动释放。

我会考虑两种选择 - 将方法转换为 C 函数或将返回值放入 NSObject(例如 UIImage)。

编辑:

CGImageRef CreateRotatedImage(CGImageRef imageRef, UIImageOrientation toOrientation) {
[... your code ...]
}

调用:

CGImageRef rotatedImage = CreateRotatedImage(image, orientation);

关于ios - iOS 上的 CGImage 旋转方法内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9394422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com