Note that there are some explanatory texts on larger screens.

plurals
  1. POAsync image in button issue
    primarykey
    data
    text
    <p>I have a piece of code like this in my function. And I do a looping creating a async image inside the button. But when I run it, my button do not show the async image.Please tell me what's wrong with my code. When I print NSLog the url, its going well and show all of the image url.</p> <pre><code> // ADD BUTTON UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setFrame:CGRectMake(topLeft.x,topLeft.y,width,height)]; //Asyncronous Image NSMutableString *imageUrl; if ([[[type objectAtIndex:btnNumber] objectForKey:@"LAST_CONTENT_IMAGE"] isEqualToString:@""] || [[type objectAtIndex:btnNumber] objectForKey:@"LAST_CONTENT_IMAGE"] == nil) { imageUrl = [NSMutableString stringWithFormat:@"bank_images/actual/"]; }else{ imageUrl = [[type objectAtIndex:btnNumber] objectForKey:@"LAST_CONTENT_IMAGE"]; } NSURL *url1 = [NSURL URLWithString:imageUrl]; NSLog(@"URL GBR : %@", url1); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *stringSub = [imageUrl substringWithRange:NSMakeRange (imageUrl.length-9, 9)]; NSString *path = [NSString stringWithFormat:@"%@/%u_%@", documentsDirectory,[GlobalVariable sharedInstance].indexCategoryGlobal,stringSub]; if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { NSData *img = [NSData dataWithContentsOfFile:path]; UIImage *imageExist = [UIImage imageWithData:img]; image = imageExist; [btn setImage:image forState:UIControlStateNormal]; }else{ dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_async(queue, ^{ //This is what you will load lazily NSData *data1 = [NSData dataWithContentsOfURL:url1]; dispatch_sync(dispatch_get_main_queue(), ^{ image = [UIImage imageWithData:data1]; [btn setImage:image forState:UIControlStateNormal]; if ([stringSub isEqualToString:@"s/actual/"]){ image = [UIImage imageNamed:@"default_thumb.png"]; [btn setImage:image forState:UIControlStateNormal]; } [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil]; }); }); } [btn setTag:btnNumber]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload