UIImageView.image = mImage泄漏

发布于 2024-11-05 00:56:25 字数 514 浏览 5 评论 0原文

我有 thread2 循环,我在其中进行组装(从原始字节数据创建)一些 UIImage 在此循环的每次迭代中

thread2loop()
{
  //make UIIamge here

  [self performSelectorOnMainThread:@selector(setUiImage) withObject:nil waitUntilDone:YES];     
}

,然后我在主线程上调用 setUIImage 方法

- (void) setUiImage
{
    self.imageView.image = nil;     
    self.imageView.image = mImage;  
    [mImage release];    
}

,它正在工作,但 Instruments 、leaks 应用程序向我显示,有 UIImage 在这里泄漏,我不知道如何@#$!摆脱它! (我很伤心,有点累 和无聊),帮助,该怎么办,tnx

I have thread2 loop where i do assembly (create from raw bytes data) some UIImage
in every iteration of this loop

thread2loop()
{
  //make UIIamge here

  [self performSelectorOnMainThread:@selector(setUiImage) withObject:nil waitUntilDone:YES];     
}

there and then i call setUIImage method on the main thread

- (void) setUiImage
{
    self.imageView.image = nil;     
    self.imageView.image = mImage;  
    [mImage release];    
}

it is working but the Instruments , leaks application shows to me that there are
UIImage leaks here and i do not know how to @#$! get rid of it! (im sad and little tired
and bored), help, what to do, tnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-11-12 00:56:25

用...包围你的线程代码

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//threaded code....

[pool release];

Surround your threaded code with...

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//threaded code....

[pool release];
反话 2024-11-12 00:56:25

经典的生产者/消费者问题。您的生产者线程可能超过主线程(消费者)。我建议保留一个图像队列(而不是单个 mImage),由将图像入队(从后台队列)的锁保护,并将图像从主队列中出队。或者您可以使用 GCD,这使得这变得更加容易。您可以使用一个块来保留图像,然后将其设置在主队列中的图像视图上,而不是使用 mImage 来保存创建的图像。例如:

thread2loop() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    while (...) {
        __block id self_block = self; // (don't want to retain self in the block)
        UIImage *img = [[UIImage alloc] initWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationUp];

        dispatch_async(dispatch_get_main_queue(), ^{
            block_self.imageView.image = img;  
            [img release];
        }); 
    }
    [pool drain]; // release is outdated for autorelease pools
}

警告:这样做太多会很快导致设备内存不足并导致您的应用程序被终止。您可能希望确保此技术的使用仅限于创建少量图像。

Classic producer/consumer problem. Your producer thread is probably outrunning the main thread (the consumer). I'd recommend keeping a queue of images (instead of the single mImage), guarded by a lock which you enqueue images onto (from your background queue), and dequeue images from your main queue. Or you could use GCD, which makes this even easier. Instead of using mImage to hold onto the created image, you could just use a block which would retain the image and then set it on your image view in the main queue. Something like:

thread2loop() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    while (...) {
        __block id self_block = self; // (don't want to retain self in the block)
        UIImage *img = [[UIImage alloc] initWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationUp];

        dispatch_async(dispatch_get_main_queue(), ^{
            block_self.imageView.image = img;  
            [img release];
        }); 
    }
    [pool drain]; // release is outdated for autorelease pools
}

Warning: Doing this too much will quickly run the device out of memory and cause your app to be killed. You probably want to make sure that your use of this technique is limited to creating a small number of images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文