objective c - How to get dynamicaly height of image in dispatch_async method in scrollview -
i using below code images server. want dynamicaly height of image , add image in scrollview.
from below code when height outside dispatch_async method shows zero.
how can dynamically height of image async image load.
- (void)viewdidload { [self loadviewpublicevents]; } -(void) loadviewpublicevents { (int i=0;i<arraypublicevents.count;i++) { uiimageview *img_vw1=[[uiimageview alloc] init]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ nsdata *imagedata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:[nsstring stringwithformat:@"http://abc.us/uploads/event/%@",[[arraypublicevents objectatindex:i] valueforkey:@"image"]]]]; uiimage *images = [[uiimage alloc]initwithdata:imagedata]; dispatch_async(dispatch_get_main_queue(), ^{ img_vw1.image = images; scaledheight = images.size.height; }); }); nslog(@"%f",scaledheight); // print 0 img_vw1.backgroundcolor=[uicolor clearcolor]; img_vw1.frame=cgrectmake(0,y+5,screen_width,197); [img_vw1 setcontentmode:uiviewcontentmodescaleaspectfit]; img_vw1.backgroundcolor=[uicolor clearcolor]; [self.scrll_vw addsubview:img_vw1]; } }
thanks in advance
your code:
nslog(@"%f",scaledheight); // print 0 img_vw1.backgroundcolor=[uicolor clearcolor]; img_vw1.frame=cgrectmake(0,y+5,screen_width,197); [img_vw1 setcontentmode:uiviewcontentmodescaleaspectfit]; img_vw1.backgroundcolor=[uicolor clearcolor]; [self.scrll_vw addsubview:img_vw1];
is being executed, before loaded image.
thus have either wait (herefore use semaphores until thread has finished) or place inside of block.
as want modify ui makes sense place main block:
uiimageview *img_vw1=[[uiimageview alloc] init]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ nsdata *imagedata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:[nsstring stringwithformat:@"http://abc.us/uploads/event/%@",[[arraypublicevents objectatindex:i] valueforkey:@"image"]]]]; uiimage *images = [[uiimage alloc]initwithdata:imagedata]; dispatch_async(dispatch_get_main_queue(), ^{ img_vw1.image = images; scaledheight = images.size.height; nslog(@"%f",scaledheight); // print 0 img_vw1.backgroundcolor=[uicolor clearcolor]; img_vw1.frame=cgrectmake(0,y+5,screen_width,197); [img_vw1 setcontentmode:uiviewcontentmodescaleaspectfit]; img_vw1.backgroundcolor=[uicolor clearcolor]; [self.scrll_vw addsubview:img_vw1]; }); });
for more information, here link apple's documentation: https://developer.apple.com/library/content/documentation/general/conceptual/concurrencyprogrammingguide/operationqueues/operationqueues.html
Comments
Post a Comment