A warning - and an invite for other developers to verify and submit a bug report. Starting with watchOS4, and now also in watchOS4.1 beta, there's a pretty severe memory leak if you keep loading images into an interface image object on the watch. I've submitted this as a bug 34773611.
My test code is this:
@property (strong, nonatomic) IBOutlet WKInterfaceImage *uiImage;
- (void)willActivate {
[super willActivate];
[self loadImage];
}
NSString *TEST_IMAGE_URL = @"https://lorempixel.com/400/224/";
NSURLSessionDataTask *imageDataDownloadTask;
- (void)loadImage {
NSLog(@"loadImage");
imageDataDownloadTask = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:TEST_IMAGE_URL] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (data != nil && error == nil) {
NSLog(@"Image Loaded.");
// First way of displaying image, causes larger memory leak
UIImage *loadedImage = [UIImage imageWithData:data];
[self.uiImage setImage:loadedImage];
// Second way of displaying the image, causes smaller memory leak
//[self.uiImage setImageData:data];
[self loadImage];
}
}];
[imageDataDownloadTask resume];
}
The test image URL above provides a random image to load every time, and by doing this in the code above, you can see the memory increases at about 0.3MB per each loaded image, when the data is first converted into a UIImage, or at a rate of about 0.1MB per loaded image if you do the direct setImageData:data approach (so make sure to use this approach if in any way possible). This definitely didn't happen in watchOS3.2, and I'm pretty sure it didn't happen in the early watchOS4 beta either. Either way now it definitely happens - I only wish I discovered and reported it earlier.