简体   繁体   English

在iPhone 4 / 4S和iPad 2上生成5400万像素的图像

[英]Generating a 54 megapixel image on iPhone 4/4S and iPad 2

I'm currently working on a project that must generate a collage of a 9000x6000 pixels resolution, generated from 15 photos . 我目前正在开发一个项目,必须生成一张9000x6000像素分辨率的拼贴画 ,由15张照片生成。 The problem that I'm facing is that when I finish drawing I'm getting an empty image (those 15 images are not being drawn in the context). 我面临的问题是,当我完成绘图时,我得到一个空图像(这15张图像没有在上下文中绘制)。

This problem is only present on devices with 512MB of RAM like iPhone 4/4S or iPad 2 and I think that this is a problem caused by the system because it cannot allocate enough memory for this app. 此问题仅出现在具有512MB RAM(如iPhone 4 / 4S或iPad 2)的设备上,我认为这是由系统引起的问题,因为它无法为此应用分配足够的内存。 When I run this line: UIGraphicsBeginImageContextWithOptions(outputSize, opaque, 1.0f); 当我运行这一行时: UIGraphicsBeginImageContextWithOptions(outputSize, opaque, 1.0f); the app's memory usage raises by 216MB and the total memory usage gets to ~240MB RAM . 该应用程序的内存使用量增加了216MB ,总内存使用量达到~240MB RAM

The thing that I cannot understand is why on Earth the images that I'm trying to draw within the for loop are not being rendered always on the currentContext ? 我无法理解的是为什么在地球上我试图在for loop中绘制的图像不会始终currentContext上渲染? I emphasized the word always , because only once in 30 tests the images were rendered (without changing any line of code). 总是强调这个词,因为在30次测试中只有一次渲染图像(不改变任何代码行)。

Question nr. 问题是 2 : If this is a problem caused by the system because it cannot allocate enough memory, is there any other way to generate this image, like a CGContextRef backed by a file output stream, so that it won't keep the image in the memory? 2 :如果这是系统引起的问题,因为它无法分配足够的内存,是否有其他方法可以生成此图像,如文件输出流支持的CGContextRef,这样它就不会将图像保留在内存中?

This is the code : 这是代码

CGSize outputSize = CGSizeMake(9000, 6000);
BOOL opaque = YES;
UIGraphicsBeginImageContextWithOptions(outputSize, opaque, 1.0f);

CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(currentContext, [UIColor blackColor].CGColor);
CGContextFillRect(currentContext, CGRectMake(0, 0, outputSize.width, outputSize.height));

for (NSUInteger i = 0; i < strongSelf.images.count; i++)
{
    @autoreleasepool
    {
        AGAutoCollageImageData *imageData = (AGAutoCollageImageData *)strongSelf.layout.images[i];

        CGRect destinationRect = CGRectMake(floorf(imageData.destinationRectangle.origin.x * scaleXRatio),
                                            floorf(imageData.destinationRectangle.origin.y * scaleYRatio),
                                            floorf(imageData.destinationRectangle.size.width * scaleXRatio),
                                            floorf(imageData.destinationRectangle.size.height * scaleYRatio));

        CGRect sourceRect = imageData.sourceRectangle;

        // Draw clipped image
        CGImageRef clippedImageRef = CGImageCreateWithImageInRect(((ALAsset *)strongSelf.images[i]).defaultRepresentation.fullScreenImage, sourceRect);
        CGContextDrawImage(currentContext, drawRect, clippedImageRef);
        CGImageRelease(clippedImageRef);
    }
}

// Pull the image from our context
strongSelf.result = UIGraphicsGetImageFromCurrentImageContext();

// Pop the context
UIGraphicsEndImageContext();

PS: The console doesn't show anything but ' memory warnings ', which are expected to see. PS:控制台除了“ 内存警告 ”外没有显示任何内容,预计会看到。

Sound like a cool project. 听起来像一个很酷的项目。

Tactic : try also releasing imageData at the end of every loop (explicitly, after releasing the clippedImageRef ) Tactic :尝试在每个循环结束时释放imageData (显式地,在释放clippedImageRef

Strategic : If you do need to support such "low" RAM requirements with such "high" input, maybe you should consider 2 alternative options: 战略性 :如果您确实需要通过这种“高”输入来支持这种“低”RAM要求,那么您应该考虑两种备选方案:

  1. Compress (obviously): even minimal, naked to the eye, JPEG compression can go a long way. 压缩(显然):即使是最小的,裸眼,JPEG压缩也可以走很长的路。
  2. Split: never "really" merge the image. 拆分:从不“真正”合并图像。 Have an arrayed datastructure which represents a BigImage . 有一个表示BigImage的阵列数据结构。 make utilities for the presentation logic. 为表示逻辑制作实用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM