Search code examples
iphoneiosios4assetslibrary

writeImageDataToSavedPhotosAlbum Slows Dramatically After A Few Uses


I save images to the iPhone's photo album using the api "writeImageDataToSavedPhotosAlbum". However, after several uses (such as 4), it begins to take approximately twice as long each time to save.

I have created a test method to reproduce it, where it saves the exact same image each time. The method is as follows:

-(IBAction)testButton {
    NSData *data = [NSData dataWithContentsOfFile:[[self photosDirectory] stringByAppendingPathComponent:[[self contentsOfPhotoDirectory] objectAtIndex:0]]];
    ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
    __block NSDate *date = [[NSDate date] retain];
    [al writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        NSLog(@"Saving Time: %g", [[NSDate date] timeIntervalSinceDate:date]);
        [date release];
    }];
    [al release];
}

On a freshly restarted iPhone 4, I get the following save times.

2011-06-01 21:23:13.641 myapp[95:707] Saving Time: 5.30819
2011-06-01 21:23:17.101 myapp[95:707] Saving Time: 1.5311
2011-06-01 21:23:21.916 myapp[95:707] Saving Time: 2.52412
2011-06-01 21:23:25.974 myapp[95:707] Saving Time: 2.85623
2011-06-01 21:23:32.275 myapp[95:707] Saving Time: 4.93484
2011-06-01 21:23:42.024 myapp[95:707] Saving Time: 7.93288
2011-06-01 21:24:00.317 myapp[95:707] Saving Time: 15.8561
2011-06-01 21:24:33.199 myapp[95:707] Saving Time: 29.7571

What is wrong?


Solution

  • Possibly the problem is that you are using memory and not freeing it, so later saves generate a lot of memory warnings... this is exactly the kind of thing the Time Profiler and Object Alloc instruments should be able to help you with, figuring out why it slows after just a few images.

    In my own app I do not think I saw that much of a slowdown after repeated shots.