绘制一系列UIImage和内存问题 [英] Drawing a series of UIImages and memory issues
问题描述
我需要在彼此之间绘制一系列大的UIImage,我遇到了内存问题,我想知道是否有人有解决方案.
I need to draw a series of large UIImages on top of each other and I am coming up against memory issues and I was wondering if anybody has a solution.
下面的代码为您提供了有关我的函数调用工作方式的示例.功能已简化以显示我的主要问题.
The code below gives you an example of how my function calls work. The functions have been simplified to show my main issue.
函数"generateImage"具有一个for循环,该循环调用一个子类,该子类还返回从一系列路径生成的图像.我在调用周围放置了@autoreleasepools,以便生成的图像在使用后立即被释放,但这似乎不起作用,并且当arrayOfSubImages增长时,我遇到了内存问题.
The function "generateImage" has a for loop that calls into a subclass that also returns images that have been generated from a series of paths. I have put @autoreleasepools around the calls so that the images that are generated are immediately deallocated after they are used, but this does not seem to be working and when arrayOfSubImages grows I come up against memory issues.
-( UIImage *) generateImage
{
@autoreleasepool {
UIGraphicsBeginImageContextWithOptions( CGSizeMake( 1000, 1000 ), NO, 0.0 );
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
for ( SubImage *subImage in arrayOfSubImages ) {
UIImage *aImage = [ subImage imageFromPaths ];
[ aImage drawAtPoint:CGPointZero ];
}
UIGraphicsPopContext();
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
从generateImage调用的SubImage
The SubImage that is called from generateImage
-( UIImage *) imageFromPaths
{
@autoreleasepool {
UIGraphicsBeginImageContextWithOptions( CGSizeMake( 1000, 1000 ), NO, 0.0 );
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
for ( UIBezierPath *path in arrayOfPaths ) {
[ path fill ];
}
UIGraphicsPopContext();
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
任何帮助将不胜感激
Reza
推荐答案
我过去也遇到过类似的问题.尝试在for循环中添加一个@autoreleasepool ,因为在紧密循环中执行多个占用大量内存的操作很可能会遇到内存问题.直到循环结束,内存才会自动释放自身,即使该循环包含在自动释放池中也是如此-您需要释放每个单独的迭代.
I've had similar problems in the past. Try adding an @autoreleasepool within your for-loops because you're likely running into memory problems by performing several memory intensive operations within a tight loop. Memory won't automatically release itself until a loop is over even if that loop is contained in an autoreleasepool -- you need to release each individual iteration.
这篇关于绘制一系列UIImage和内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!