Переглянути джерело

potentially problem in low memory device

ibireme 9 роки тому
батько
коміт
0524b61698
1 змінених файлів з 16 додано та 7 видалено
  1. 16 7
      YYImage/YYAnimatedImageView.m

+ 16 - 7
YYImage/YYAnimatedImageView.m

@@ -135,7 +135,7 @@ typedef NS_ENUM(NSUInteger, YYAnimatedImageType) {
     NSUInteger _curIndex; ///< current frame index (from 0)
     NSUInteger _totalFrameCount; ///< total frame count
     
-    BOOL _loopEnd; ///< weather the loop is end.
+    BOOL _loopEnd; ///< whether the loop is end.
     NSUInteger _curLoop; ///< current loop count (from 0)
     NSUInteger _totalLoop; ///< total loop count, 0 means infinity
     
@@ -165,22 +165,28 @@ typedef NS_ENUM(NSUInteger, YYAnimatedImageType) {
     if ([self isCancelled]) return;
     view->_incrBufferCount++;
     if (view->_incrBufferCount == 0) [view calcMaxBufferCount];
-    if ((int)view->_incrBufferCount > (int)view->_maxBufferCount) {
+    if (view->_incrBufferCount > (NSInteger)view->_maxBufferCount) {
         view->_incrBufferCount = view->_maxBufferCount;
     }
     NSUInteger idx = _nextIndex;
     NSUInteger max = view->_incrBufferCount < 1 ? 1 : view->_incrBufferCount;
     NSUInteger total = view->_totalFrameCount;
+    view = nil;
+    
     for (int i = 0; i < max; i++, idx++) {
         @autoreleasepool {
             if (idx >= total) idx = 0;
             if ([self isCancelled]) break;
+            view = _view;
             LOCK_VIEW(BOOL miss = (view->_buffer[@(idx)] == nil));
+            view = nil;
             if (miss) {
                 UIImage *img = [_curImage animatedImageFrameAtIndex:idx];
-                img = [img yy_imageByDecoded];
+                img = img.yy_imageByDecoded;
                 if ([self isCancelled]) break;
+                view = _view;
                 LOCK_VIEW(view->_buffer[@(idx)] = img ? img : [NSNull null]);
+                view = nil;
             }
         }
     }
@@ -373,16 +379,18 @@ typedef NS_ENUM(NSUInteger, YYAnimatedImageType) {
 
 // dynamically adjust buffer size for current memory.
 - (void)calcMaxBufferCount {
-    NSUInteger bytes = _curAnimatedImage.animatedImageBytesPerFrame;
-    if (bytes == 0) bytes = 1;
+    int64_t bytes = (int64_t)_curAnimatedImage.animatedImageBytesPerFrame;
+    if (bytes == 0) bytes = 1024;
     
     int64_t total = _YYDeviceMemoryTotal();
     int64_t free = _YYDeviceMemoryFree();
     int64_t max = MIN(total * 0.2, free * 0.6);
     max = MAX(max, BUFFER_SIZE);
     if (_maxBufferSize) max = max > _maxBufferSize ? _maxBufferSize : max;
-    _maxBufferCount = (float)max / (float)bytes;
-    if (_maxBufferCount == 0) _maxBufferCount = 1;
+    double maxBufferCount = (double)max / (double)bytes;
+    if (maxBufferCount < 1) maxBufferCount = 1;
+    else if (maxBufferCount > 512) maxBufferCount = 512;
+    _maxBufferCount = maxBufferCount;
 }
 
 - (void)dealloc {
@@ -398,6 +406,7 @@ typedef NS_ENUM(NSUInteger, YYAnimatedImageType) {
 
 - (void)stopAnimating {
     [super stopAnimating];
+    [_requestQueue cancelAllOperations];
     _link.paused = YES;
     self.currentIsPlayingAnimation = NO;
 }