Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatchKit: Setting WKInterfaceImage programmatically

I want to set my UIImage to WKInterfaceImage, but the simulator shows only black screen. It works OK using setImageNamed: NSString* method, but not with setImage: UIImage* . My file1.png is added to "(App Name) WatchKit App" folder.

- (void)willActivate {
   [self.imageView1 setImage: [UIImage imageNamed: @"file1"]]; // doesn't work
   [self.imageView1 setImageNamed: @"file1"]; // works OK
    [super willActivate];
}

Thanks!

like image 468
Dawid Avatar asked Mar 10 '15 01:03

Dawid


2 Answers

Everything is working as expected.

file1 is in your WatchKit App folder, which means it is on the watch.

Calling [UIImage imageNamed:] loads from the main bundle, which doesn't contain file1, so you get nil.

The way you are doing this is correct. setImageNamed: will look for images on the Watch first, then in the cache.

like image 85
Jack Avatar answered Nov 11 '22 09:11

Jack


Calling [UIImage imageNamed:imageName] from your extension will always return nil for a cached image. The image is cached on the watch — not in your extension.

Once an image is cached, it allows you to set a WKInterfaceImage using the [WKInterfaceImage setImageNamed:] method.

like image 45
gagarwal Avatar answered Nov 11 '22 11:11

gagarwal