0

i have some problem. So i have code which update song name and picture from php. Song name work and also updated but picture not work, in php file all work but in my project - no. How make update picture from url after 10 sec for example. Thanks.

-(void)viewWillDraw {

    NSURL *artistImageURL = [NSURL URLWithString:@"http://site.ru/ParseDataField/kiss.php?image"];

    NSImage *artistImage = [[NSImage alloc] initWithContentsOfURL:artistImageURL];

    [dj setImage:artistImage];

    dispatch_queue_t queue = dispatch_get_global_queue(0,0);

    dispatch_async(queue, ^{

    NSError* error = nil;

        NSString* text = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://site.ru/ParseDataField/kiss.php?artist"]

                                               encoding:NSASCIIStringEncoding
                                                   error:&error];

        dispatch_async(dispatch_get_main_queue(), ^{

            [labelName setStringValue:text];


        });
    });
}
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
Evgenii Melnik
  • 69
  • 2
  • 14
  • Not "in Xcode". Xcode is an IDE and you can't modify it. I assume you want to do this in your OS X application? –  May 14 '13 at 09:03

1 Answers1

0

You should really consider placing this code someplace other than -viewWillDraw. This routine can be called multiple times for the same NSView under some circumstances and, more importantly, you need to call [super viewWillDraw] to make sure that things will actually draw correctly (if anything is drawn in the view itself).

For periodic updates (such as every 10 seconds), you should consider using NSTimer to trigger the retrieval of the next object.

As for the general question of why your image isn't being drawn correctly, you should probably consider putting the image retrieval and drawing code into the same structure as your label retrieval and drawing code. This will get the [dj setImage: artistImage] method call outside of the viewWillDraw chain which is likely causing some difficulty here.

gaige
  • 17,263
  • 6
  • 57
  • 68
  • When i write timer code NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 0.40 target: self selector:@selector(updated:) userInfo: nil repeats:NO]; I have very slowly work of my app, and after my app will close with some error... – Evgenii Melnik May 14 '13 at 12:13
  • There's an awful lot of variation possible in what you may have done in your `update:` routine. You are adding a .4 second delay with the timer as you have written it, so that's probably some of the delay, as may be the server's response. Hopefully you're not calling the `NSTimer` from inside of `viewWillDraw`, since that'd have similar problems to doing the work in `viewWillDraw`. – gaige May 14 '13 at 12:18