优化Nimbus网络相册的内存

时间:2013-08-05 12:37:18

标签: ios afnetworking image-caching nimbus-ios

我有一个使用视图控制器的应用程序,该视图控制器扩展NetworkPhotoAlbumViewController,后者又扩展NIToolbarPhotoViewController

基本上它实现了所有NI协议,例如NIPhotoAlbumScrollViewDataSourceNIPhotoScrubberViewDataSourceNIOperationDelegateNIPhotoAlbumScrollViewDelegate,唯一的自定义是didReceiveMemoryWarning

- (void) didReceiveMemoryWarning {
    NSLog(@"Nimbus Photo Album memory warning");
    [self.highQualityImageCache reduceMemoryUsage];
    [self.thumbnailImageCache reduceMemoryUsage];
    NSLog(@"Nimbus Photo Album end 0memory warning");
}

addOperation减少并发下载次数:

-(void) addOperation: (AFImageRequestOperation *) operation {
    NSLog(@"operation queue size is %d", _queue.operationCount);

    BOOL found = NO;
    for (AFImageRequestOperation *op in _queue.operations) {
        if(!operation.isCancelled && [[operation.request.URL absoluteString] compare:[op.request.URL absoluteString]] == NSOrderedSame) {
            NSLog(@"found the same operation");
            found = YES;
            break;
        }
    }

    if(found) return;

    NSInteger cancelledCount = 0;
    if (_queue.operationCount > 5) {
        for (NSOperation *operation in _queue.operations) {
            if (operation.isCancelled) {
                cancelledCount ++;
            }
        }
    }

    NSLog(@"cancelled operation count is %d", cancelledCount);
    NSInteger count = _queue.operationCount;
    if(count - cancelledCount > 5) {
        for (NSOperation *operation in _queue.operations) {
            if(!operation.isCancelled) {
                NSLog(@"cancel operation");
                [operation cancel];
                break;
            }
        }
    }

    NSLog(@"operation is added into queue");
    [_queue addOperation:operation];
}

该应用程序还使用AVFoundation捕获照片,这些照片会被完全发送到服务器(稍后会加载到网络相册中)。

问题在于,如果我使用NetworkPhotoAlbumViewController然后切换到照片捕获,应用程序通常会因内存不足而崩溃(应用程序内存可能达到20到30 MB之间),即使{{1 }}和didReceiveMemoryWarning被调用。

我是否可能出错并且内存未正确清除?问题是由reduceMemoryUsage引起的吗?还有哪些替代方法是从网上下载一些图像并将它们显示在带有缩略图加载和平移和缩放的相册中?

0 个答案:

没有答案