objective c - How do I load images with dispatch queue for screensavers? -


i trying code screensaver loads images website , displays them in random order. randomness , image acquiring covered web api, how downloading without freezing ui has gotten me stuck.

currently code looks this:

- (void)animateoneframe {     nsrect viewbounds = [self bounds];     dispatch_queue_t queue = dispatch_get_current_queue();      dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{          nsimage *image = [self extractimagefrom:@"my_url.com"];         nssize imagesize = [image size];                     nsrect imagerect = nsmakerect( 0, 0, imagesize.width, imagesize.height );          dispatch_sync(queue, ^{                 [image drawinrect:viewbounds fromrect:imagerect operation:nscompositecopy fraction:1.0];         });      });     return; } 

here tried trigger function gets image , caption on separate queue. image drawn on original view, issue.

whenever select final screenviewer plugin in preference panel, image drawn on preference panel's view (not black preview rectangle). similarly, when previewing screensaver image not drawn, whenever quit screensaver preference panel's view covered image.

it seems me image being loaded , drawn, not in right queue/context? instead of using variable queue used dispatch_get_main_queue() identical results. appreciated.

it seems there nothing wrong threading. it's drawing problem.

your drawing call without context. drawinrect quartz function needs graphic context. used in drawing methods of nsview.

you should subclass view drawing , give method updatewithimage:(nsimage*)image drawing properly. call method on main thread.

    dispatch_sync(dispatch_get_main_queue(), ^{             [thecorrectview updatewithimage:image];     }); 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -