如何在使用 CGImageDestinationFinalize 时避免 malloc [英] How to avoid malloc while using CGImageDestinationFinalize

查看:20
本文介绍了如何在使用 CGImageDestinationFinalize 时避免 malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下堆栈问题以编程方式在 iOS 中创建 GIF:

I am trying to programmatically create GIF in iOS, using the following stack's question:

通过 iOS 创建和导出动画 gif?

我的代码如下所示:

 // File Parameters
const void *keys[] =   { kCGImagePropertyGIFLoopCount };
const void *values[] = { (CFNumberRef) 0 };

CFDictionaryRef params          = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
const void *keys2[] =   { kCGImagePropertyGIFDictionary };
const void *values2[] = { (CFDictionaryRef) params  };

CFDictionaryRef fileProperties  = CFDictionaryCreate(NULL, keys2 , values2, 1, NULL, NULL);

// URL to the documents directory
NSURL *documentsDirectoryURL  = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL                = [documentsDirectoryURL URLByAppendingPathComponent:fileName];

// Object that writes GIF to the specified URL
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, [arrayOfAllFrames count], NULL);
CGImageDestinationSetProperties(destination, fileProperties);

for (NSUInteger i = 0; i < [arrayOfAllFrames count]; i++) {
    @autoreleasepool {
        float delayTime = [[gifFramesDuration objectAtIndex:i] floatValue]; 
        NSDictionary *frameProperties = @{
                                          (__bridge id)kCGImagePropertyGIFDictionary: @{
                                                  (__bridge id)kCGImagePropertyGIFDelayTime: [NSNumber numberWithFloat:delayTime] // a float (not double!) in seconds, rounded to centiseconds in the GIF data
                                                  }
                                          };

        UIImage *myImage = [arrayOfAllFrames objectAtIndex:i];
        CGImageDestinationAddImage(destination, myImage.CGImage, (__bridge CFDictionaryRef)frameProperties);
    }
}
if (!CGImageDestinationFinalize(destination)) {
    NSLog(@"failed to finalize image destination");
}
CFRelease(destination);
CFRelease(fileProperties);
CFRelease(params);

但是,一旦我尝试向 GIF 文件添加大约 240 帧,一旦调用 CGImageDestinationFinalize,调试器就会抛出以下错误:

However once I try to add around 240 frames to the GIF file, debugger throws the following error once the CGImageDestinationFinalize gets called:

(923,0xb0115000) malloc: *** error for object 0xd1e7204: incorrect checksum for freed object - object was probably modified after being freed.

能否请您为我提供一些解决方法,或者提供有关如何避免 malloc 的建议?

Could you please provide me with some workaround, or with a suggestion on how to avoid malloc?

推荐答案

首先,尝试使用 Instruments 调试您的应用.您可能会注意到问题是由以下方法引起的:从 rgbimagewu 生成

First of all, try debugging your app using Instruments. You probably will notice that the problem is caused by the method: Generatefromrgbimagewu

我一直想知道原因是否在我的线程实现中,但事实证明,一旦出现此类错误,您应该专注于调整图像大小.

I have been wondering whether the cause was in my threads implementation, but it turns out, that once you have that kind of error, you should focus on resizing the Image.

调整图像大小后,上面发布的代码将生成您自己的 GIF.

Once the image had been resized, the code published above, will generate your own GIF.

这篇关于如何在使用 CGImageDestinationFinalize 时避免 malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆