UIImageView.image = mImage泄漏
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用...包围你的线程代码
Surround your threaded code with...
经典的生产者/消费者问题。您的生产者线程可能超过主线程(消费者)。我建议保留一个图像队列(而不是单个 mImage),由将图像入队(从后台队列)的锁保护,并将图像从主队列中出队。或者您可以使用 GCD,这使得这变得更加容易。您可以使用一个块来保留图像,然后将其设置在主队列中的图像视图上,而不是使用 mImage 来保存创建的图像。例如:
警告:这样做太多会很快导致设备内存不足并导致您的应用程序被终止。您可能希望确保此技术的使用仅限于创建少量图像。
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:
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.