Optimizing memory for Nimbus network album

132 views Asked by At

I have an application which uses a view controller which extends NetworkPhotoAlbumViewController, which in turn extends NIToolbarPhotoViewController

Basically it implements all the NI protocols, such as NIPhotoAlbumScrollViewDataSource, NIPhotoScrubberViewDataSource, NIOperationDelegate, NIPhotoAlbumScrollViewDelegate and the only customization are didReceiveMemoryWarning:

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

and addOperation to reduce number of concurrent downloads:

-(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];
}

The application also uses AVFoundation for capturing photos, which are sent full res to server (and loaded later in the network album).

The problem is that if I use NetworkPhotoAlbumViewController and then switch to photo capture, quite often the application crashes because of lack of memory (app memory can reach something between 20 and 30 MB), even if didReceiveMemoryWarning and reduceMemoryUsage are called.

Is it possible that I am doing something wrong and the memory is not cleared correctly? Is the problem caused by AFNetworking? And what alternatives are to downloading some images from the web and displaying them in a photo album with thumbnail loading and panning and zooming?

0

There are 0 answers