我使用以下代码下载许多图片,并在下载后更新主线程中的imageView。
下载一张图像的代码(这是为每次图像下载执行的代码):
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//download image. Code to download image
UIImage * tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myUrl]]];
//if image downloaded
if (tempImage)
{
// Run code to set image to UIImageView in main thread.
dispatch_async(dispatch_get_main_queue(), ^{
myImageView.image = tempImage;
[myImageView setNeedsDisplay];
});
}
});
我在dispatch_get_main_queue()
上获得代码= 1的EXC_BAD_ACCESS。
我认为tempImage
是自动释放的,可能在调用主线程之前被释放,但我不确定这是否是这次特定崩溃的问题。
我正在使用Xcode 6.1(可能不是Xcode问题)而且它在iOS 8.1上崩溃了
记录线程98(在线程98崩溃)
#0 objc_retain ()
#1 __84-[MyController getTableCell:withCell:withIndexPath:]_block_invoke
#2 _dispatch_call_block_and_release ()
#3 _dispatch_client_callout ()
#4 _dispatch_root_queue_drain ()
#5 _dispatch_worker_thread2 ()
#6 _pthread_wqthread ()
#7 start_wqthread ()
Enqueued from com.apple.main-thread (Thread 1)Queue : com.apple.main-thread (serial)
#0 _dispatch_async_f_slow ()
#1 -[My_Controller getTableCell:withCell:withIndexPath:]
#2 -[My_Controller tableView:cellForRowAtIndexPath:]
#3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] ()
#4 -[_UITableViewUpdateSupport(Private) _setupAnimationsForNewlyInsertedCells] ()
#5 -[_UITableViewUpdateSupport _setupAnimations] ()
#6 -[UITableView _updateWithItems:updateSupport:] ()
#7 -[UITableView _endCellAnimationsWithContext:] ()
主线程:
#0 __mmap ()
#1 mmap ()
#2 ImageIO_Malloc ()
#3 copyImageBlockSetPNG ()
#4 ImageProviderCopyImageBlockSetCallback ()
#5 CA::Render::create_image(CGImage*, CGColorSpace*, unsigned int) ()
#6 CA::Render::copy_image(CGImage*, CGColorSpace*, unsigned int, double) ()
#7 CA::Render::prepare_image(CGImage*, CGColorSpace*, unsigned int, double) ()
#8 CA::Layer::prepare_commit(CA::Transaction*) ()
#9 CA::Context::commit_transaction(CA::Transaction*) ()
#10 CA::Transaction::commit() ()
#11 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) ()
#12 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#13 __CFRunLoopDoObservers ()
#14 __CFRunLoopRun ()
#15 CFRunLoopRunSpecific ()
#16 GSEventRunModal ()
#17 UIApplicationMain ()
#18 main