Frage

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];


        });
    });
}
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top