2013-12-11 74 views
0

我正在使用此代碼在ios上的註釋視圖中加載圖像,但經常在沒有錯誤消息的annotationView.image = img;行上應用程序崩潰任何人都可以幫助我嗎?應用程序卡在註釋視圖

dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0); 
    dispatch_async(backgroundQueue, ^{ 
     NSString *url = [[NSString alloc]initWithString:[dict valueForKey:@"user_image_thumb"]]; 
     NSString *trimurl=[url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
     UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:trimurl]]]; 
     if (img!=nil) { 
      annotationView.image = img; 
     } 
     dispatch_async(dispatch_get_main_queue(), ^{ 
     }); 
}); 

回答

0

如何

dispatch_async(dispatch_get_main_queue(), ^{ 
    if (img) {         
     annotationView.image = img; 
    } 
}); 
0

@tuss是正確的嘗試:

dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0); 
dispatch_async(backgroundQueue, ^{ 
    NSString *url = [[NSString alloc]initWithString:[dict valueForKey:@"user_image_thumb"]]; 
    NSString *trimurl=[url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:trimurl]]]; 

    dispatch_async(dispatch_get_main_queue(), ^{if (img!=nil) { 
     annotationView.image = img; 
    } 
    }); 

});

相關問題