我正在开发iOS应用。我需要裁剪从PDF生成的图像。 有时,图像分辨率可能非常大 我使用跟随代码生成裁剪图像。我的问题是内存一直在增加。从未发布。
- (UIImage *)croppedImageWithFrame:(CGRect)frame angle:(NSInteger)angle{
UIImage *croppedImage = nil;
CGPoint drawPoint = CGPointZero;
UIGraphicsBeginImageContextWithOptions(frame.size, YES, self.scale);
{
CGContextRef context = UIGraphicsGetCurrentContext();
//To conserve memory in not needing to completely re-render the image re-rotated,
//map the image to a view and then use Core Animation to manipulate its rotation
if (angle != 0) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:self];
imageView.layer.minificationFilter = @"nearest";
imageView.layer.magnificationFilter = @"neareset";
imageView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angle * (M_PI/180.0f));
CGRect rotatedRect = CGRectApplyAffineTransform(imageView.bounds, imageView.transform);
UIView *containerView = [[UIView alloc] initWithFrame:(CGRect){CGPointZero, rotatedRect.size}];
[containerView addSubview:imageView];
imageView.center = containerView.center;
CGContextTranslateCTM(context, -frame.origin.x, -frame.origin.y);
[containerView.layer renderInContext:context];
}
else {
CGContextTranslateCTM(context, -frame.origin.x, -frame.origin.y);
[self drawAtPoint:drawPoint];
}
croppedImage = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
return croppedImage;
}
当我调试时, 它在线燃烧100MB
UIGraphicsBeginImageContextWithOptions(frame.size, YES, self.scale);
然后当遇到跟进线时,再烧掉150MB
[self drawAtPoint:drawPoint];
当运行到跟随行时,它会释放100MB
UIGraphicsEndImageContext();
完成后,150MB从未发布
我认为UIGraphicsEndImageContext()应该释放所有250MB。为什么不呢?