views:

307

answers:

1

I am writing a simple application for an artist who wants to market his work on iTunes. Because there are only twelve images, which can be deleted more rapidly than they could be selected from an image picker, the user does not select the images individually. Instead, the images are copied programmatically from the application bundle into the photo library. This minimizes the size and complexity of the application and saves the end-user from the tedium of having to select each image individually.

Note: this problem occurs only on the physical device (iPhone 3Gs/3.1).

Not surprisingly, my implementation involves calling UIImageWriteToSavedPhotosAlbum inside a loop that iterates over an array of 12 full paths to the corresponding image files (all are JPEGs). Minus the details (e.g., error checking, logging, optional callback parameters), the loop boils down to:

for ( NSString* path in paths ) {
    image = [UIImage imageWithContentsOfFile:path];
    if (image) {
        [NSThread sleepForTimeInterval:1.0]; // unacceptable
        UIImageWriteToSavedPhotosAlbum(image, ...);
    }
}

Note the line marked "unacceptable." Why must the current thread sleep for a full second prior to each invocation of UIImageWriteToSavedPhotosAlbum(image, ...)? Well, because that is quite literally how long it takes for iPhoneOS 3.1 (running on my 3Gs device) to get around to the business of actually writing a buffered UIImage object to the persistent store that represents the user's photo library! How do I know this? Well, because without the call to NSThread sleepForTimeInterval:, no error of any kind is ever reported even though only a fraction of the 12 images (which appears to vary randomly between 1 and 9) are in fact copied to the photo library. In addition, results do improve as the sleep interval increases, but, as I have discovered, the interval must be increased dramatically before the improvement becomes noticeable. Even at as high a value as 0.8 seconds, I have observed missing files following repeated trials. At 1 second, I have observed up to three consecutive "correct" runs in which all files are fully transferred before losing all patience.

I have googled this issue to death, checked every posting on this board that mentions UIImageWriteToSavedPhotosAlbum, and turned up nothing. Am I the only person in the world who has ever called that function inside a loop? Is there some reason why it should be self-evident to me that I should not do so?

A: 

A quick search of the developer forums at Apple turned up the following post:

https://devforums.apple.com/message/112119#112119 for the full thread.

This thread suggests to me that there is no safe way to write an image to the photo library programmatically.

Ben