浏览代码

detail and now playing view: do not block the main thread when loading thumbnails and micro-optimize the code

Felix Paul Kühne 10 年之前
父节点
当前提交
30d5e6f875

+ 10 - 5
VLC for iOS WatchKit Extension/VLCDetailInterfaceController.m

@@ -35,7 +35,6 @@
 
     [self addNowPlayingMenu];
     [self configureWithFile:context];
-
 }
 
 - (void)willActivate {
@@ -68,14 +67,20 @@
     BOOL playEnabled = managedObject != nil;
     self.playNowButton.enabled = playEnabled;
 
+    BOOL noProgress = (playbackProgress == 0.0 || playbackProgress == 1.0);
+    self.progressSeparator.hidden = noProgress;
+    self.progressSeparator.width = floor(playbackProgress * CGRectGetWidth([WKInterfaceDevice currentDevice].screenBounds));
+
+    /* do not block the main thread */
+    [self performSelectorInBackground:@selector(loadThumbnailForManagedObject:) withObject:managedObject];
+}
+
+- (void)loadThumbnailForManagedObject:(NSManagedObject *)managedObject
+{
     UIImage *thumbnail = [VLCThumbnailsCache thumbnailForManagedObject:managedObject];
     if (thumbnail) {
         [self.group setBackgroundImage:thumbnail];
     }
-
-    BOOL noProgress = (playbackProgress == 0.0 || playbackProgress == 1.0);
-    self.progressSeparator.hidden = noProgress;
-    self.progressSeparator.width = floor(playbackProgress * CGRectGetWidth([WKInterfaceDevice currentDevice].screenBounds));
 }
 
 - (IBAction)playNow {

+ 16 - 3
VLC for iOS WatchKit Extension/VLCNowPlayingInterfaceController.m

@@ -18,6 +18,10 @@
 #import "VLCThumbnailsCache.h"
 
 @interface VLCNowPlayingInterfaceController ()
+{
+    CGRect _screenBounds;
+    CGFloat _screenScale;
+}
 @property (nonatomic, copy) NSString *titleString;
 @property (nonatomic, copy) NSNumber *playBackDurationNumber;
 @property (nonatomic) BOOL isPlaying;
@@ -38,6 +42,10 @@
 - (void)awakeWithContext:(id)context {
     [super awakeWithContext:context];
 
+    WKInterfaceDevice *currentDevice = [WKInterfaceDevice currentDevice];
+    _screenBounds = currentDevice.screenBounds;
+    _screenScale = currentDevice.screenScale;
+
     // Configure interface objects here.
     [self requestNowPlayingInfo];
     [[VLCNotificationRelay sharedRelay] addRelayRemoteName:@"org.videolan.ios-app.nowPlayingInfoUpdate" toLocalName:@"nowPlayingInfoUpdate"];
@@ -92,9 +100,14 @@
     self.progressSeparator.width = noProgress ? 0.0 : progressWidth;
 
     self.playBackDurationNumber = duration;
-    CGFloat width = CGRectGetWidth([WKInterfaceDevice currentDevice].screenBounds);
-    CGFloat height = CGRectGetHeight([WKInterfaceDevice currentDevice].screenBounds);
-    UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0, 0, width*2, height*2) shouldReplaceCache:NO];
+
+    /* do not block */
+    [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
+}
+
+- (void)loadThumbnailForFile:(MLFile *)file
+{
+    UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0., 0., _screenBounds.size.width * _screenScale, _screenBounds.size.height * _screenScale) shouldReplaceCache:NO];
     [self.playElementsGroup setBackgroundImage:image];
 }