1

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

基本上它实现了所有的 NI 协议,例如 NIPhotoAlbumScrollViewDataSource, NIPhotoScrubberViewDataSource, NIOperationDelegateNIPhotoAlbumScrollViewDelegate唯一的自定义是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 之间),即使didReceiveMemoryWarningreduceMemoryUsage被调用。

是否有可能我做错了什么并且没有正确清除内存?问题是由 引起的AFNetworking吗?除了从网络上下载一些图像并通过缩略图加载、平移和缩放将它们显示在相册中之外,还有哪些替代方法?

4

0 回答 0