我有一个内存问题与小应用程序无限量的截图每X毫秒,并在ImageView中显示它们。即使使用自动释放,它也会非常迅速地淹没内存。下面的代码:目标C @autoreleasepool无限循环采取截图
- (void)draw {
do {
@autoreleasepool {
CGImageRef image1 = CGDisplayCreateImage(kCGDirectMainDisplay);
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:image1];
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
_imageView.image = image;
[NSThread sleepForTimeInterval:1];
}
}while(true);
}
任何想法?
你不能没有ARC使用@autoreleasepool。 – 2013-03-25 23:29:30
@RamyAlZuhouri:是的,你可以。它与ARC同时推出,但不需要ARC。 – 2013-03-25 23:39:13
由于你从来没有将控制权返回给runloop,我假设这是在后台线程上运行。您正在访问仅在主线程中允许的UI控件('_imageView')。你应该改变它为'dispatch_async(dispatch_get_main_thread(),^ {_imageView.image = image;});' – 2013-03-25 23:40:34