<bdo id='jbKf3'></bdo><ul id='jbKf3'></ul>
<i id='jbKf3'><tr id='jbKf3'><dt id='jbKf3'><q id='jbKf3'><span id='jbKf3'><b id='jbKf3'><form id='jbKf3'><ins id='jbKf3'></ins><ul id='jbKf3'></ul><sub id='jbKf3'></sub></form><legend id='jbKf3'></legend><bdo id='jbKf3'><pre id='jbKf3'><center id='jbKf3'></center></pre></bdo></b><th id='jbKf3'></th></span></q></dt></tr></i><div id='jbKf3'><tfoot id='jbKf3'></tfoot><dl id='jbKf3'><fieldset id='jbKf3'></fieldset></dl></div>

    <legend id='jbKf3'><style id='jbKf3'><dir id='jbKf3'><q id='jbKf3'></q></dir></style></legend>
    1. <tfoot id='jbKf3'></tfoot>

      <small id='jbKf3'></small><noframes id='jbKf3'>

      来自 CGImageRef 的 UIImage

      时间:2023-06-01

        <tbody id='w78c0'></tbody>

      <i id='w78c0'><tr id='w78c0'><dt id='w78c0'><q id='w78c0'><span id='w78c0'><b id='w78c0'><form id='w78c0'><ins id='w78c0'></ins><ul id='w78c0'></ul><sub id='w78c0'></sub></form><legend id='w78c0'></legend><bdo id='w78c0'><pre id='w78c0'><center id='w78c0'></center></pre></bdo></b><th id='w78c0'></th></span></q></dt></tr></i><div id='w78c0'><tfoot id='w78c0'></tfoot><dl id='w78c0'><fieldset id='w78c0'></fieldset></dl></div>
      • <bdo id='w78c0'></bdo><ul id='w78c0'></ul>

              <tfoot id='w78c0'></tfoot>

                <small id='w78c0'></small><noframes id='w78c0'>

                <legend id='w78c0'><style id='w78c0'><dir id='w78c0'><q id='w78c0'></q></dir></style></legend>
              1. 本文介绍了来自 CGImageRef 的 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在为一个更复杂的项目尝试一个简单的测试,但我很困惑为什么下面的代码会崩溃并给出 EXC_BAD_ACCESS 错误?

                I am trying a simple test for a much more complex project but I am baffled as to why the code below is crashing and giving an EXC_BAD_ACCESS error?

                这是从 UIView 调用的.

                This is called from a UIView.

                 - (void)testing {
                      NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ball.png" ofType:nil];
                      CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];
                 //   CGImageRetain(imageRef);
                
                      UIImage *newImage = [[UIImage alloc]initWithCGImage:imageRef];
                      UIImageView *iv = [[UIImageView alloc]initWithImage:newImage];
                      [self addSubview:iv];
                 }
                

                我的猜测是 CGImageRef 没有被保留,而是添加了 CGImageRetain(imageRef);没有区别.

                My guess is that the CGImageRef is not being retained but adding CGImageRetain(imageRef); makes no difference.

                我还应该注意,这个项目已经开启了 ARC.

                I should also note that this project has ARC turned on.

                编辑

                EDIT

                我做了更多的测试,发现这与 ARC 直接相关,因为我创建了 2 个基本项目,其中仅包括上面的代码.第一个关闭 ARC 并且效果很好.下一个打开 ARC 并且 BAM 崩溃并出现相同的错误.有趣的是,我只有在崩溃前第一次运行项目时才收到实际的日志错误.

                I did a little bit more testing and have discovered that this is directly related to ARC as I created 2 basic projects including only the code above. The first with ARC turned off and it worked perfectly. The next with ARC turned on and BAM crash with the same error. The interesting thing is that I got an actual log error ONLY the first time I ran the project before the crash.

                 Error: ImageIO: ImageProviderCopyImageBlockSetCallback 'ImageProviderCopyImageBlockSetCallback' header is not a CFDictionary...
                

                推荐答案

                这行就是问题所在:

                CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];
                

                创建的 UIImage 将在此完整表达式之后立即释放(例如,在此行之后).因此,即使尝试在之后添加 CGImageRetain() 也行不通.

                The created UIImage will be released immediately following this full-expression (e.g. after this line). So even trying to add a CGImageRetain() afterwards won't work.

                根本问题是CGImage返回的CGImageRef几乎肯定是UIImage的一个ivar,当UIImage 被释放.

                The fundamental problem is the CGImageRef returned from CGImage is almost certainly an ivar of the UIImage and will be released when the UIImage is deallocted.

                解决此问题的通用方法是延长 UIImage 的生命周期.您可以通过将 UIImage 放入局部变量并在最后一次引用 CGImage 之后引用它(例如,使用 (void)uiimageVar).或者,您可以将 CGImageRef 保留在同一行,如

                The generic way to fix this is to extend the lifetime of the UIImage. You can do this by placing the UIImage into a local variable and referencing it after your last reference to the CGImage (e.g. with (void)uiimageVar). Alternatively, you can retain the CGImageRef on that same line, as in

                CGImageRef imageRef = CGImageRetain([[[UIImage alloc] initWithContentsOfFile:imagePath] CGImage]);
                

                但如果你这样做了,别忘了在完成后释放 imageRef.

                But if you do this, don't forget to release the imageRef when you're done.

                这篇关于来自 CGImageRef 的 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:禁用 iCloud 同步 下一篇:iOS 启动画面的持续时间 (Default.png)

                相关文章

                最新文章

              2. <small id='1iDQZ'></small><noframes id='1iDQZ'>

                  • <bdo id='1iDQZ'></bdo><ul id='1iDQZ'></ul>

                  <i id='1iDQZ'><tr id='1iDQZ'><dt id='1iDQZ'><q id='1iDQZ'><span id='1iDQZ'><b id='1iDQZ'><form id='1iDQZ'><ins id='1iDQZ'></ins><ul id='1iDQZ'></ul><sub id='1iDQZ'></sub></form><legend id='1iDQZ'></legend><bdo id='1iDQZ'><pre id='1iDQZ'><center id='1iDQZ'></center></pre></bdo></b><th id='1iDQZ'></th></span></q></dt></tr></i><div id='1iDQZ'><tfoot id='1iDQZ'></tfoot><dl id='1iDQZ'><fieldset id='1iDQZ'></fieldset></dl></div>

                1. <tfoot id='1iDQZ'></tfoot>

                  <legend id='1iDQZ'><style id='1iDQZ'><dir id='1iDQZ'><q id='1iDQZ'></q></dir></style></legend>