3

UIImageWriteToSavedPhotosAlbum is only working sometimes. Sometimes it works, sometimes it doesn't, exact same function.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
NSLog(@"Saving image to camera roll...");
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
NSLog(@"Done!"); }

I am using a UIImagePicker controller to get the image that then calls that function.

Sometimes it saves it to the camera roll, other times it simply doesn't.

Anyone has any idea?

Thanks in advance.

Edit:

The completion method for UIImageWriteToSavedPhotosAlbum sometime returns an error of:

wait_fences: failed to receive reply: 10004003
TechZen
  • 64,370
  • 15
  • 118
  • 145
EduAlm
  • 813
  • 3
  • 11
  • 27
  • Little more detail please, Any error message? Are you sure the image is not nil? Is the method actually being called? Any other troubleshooting you have done? – TechZen Jun 10 '10 at 20:11
  • Yes it is being called, for sure, as it logs these NSLogs. And no, the image can't be nil, as it sometimes saves other times it just doesn't. – EduAlm Jun 11 '10 at 16:37
  • Did you solve the issue ? I have the same one... – Oliver Jan 27 '12 at 16:07
  • Nope, I did not. Never looked at it more tbh. – EduAlm Jan 27 '12 at 21:04

2 Answers2

17

Hope this answer isn't too late to be helpful. I ran into exactly the same problem. As it turns out the issue is two-fold. First, the image does need to be retained before it is sent to UIImageWriteToSavedPhotosAlbum. Secondly, on pre-iPhone4 devices this function can take a loooooong time to do its thing. The fix I found was to implement a callback function. See the documentation for UIImageWriteToSavedPhotosAlbum about this. The function must be in the correct format, you can't just use any old function. In this function be sure to release the image you retained or you will leak memory. You can also use this to keep track of when images are done saving. Here is my basic code below:

    -(void)processImage:(UIImage *)image {
        [image retain];
        UIImageWriteToSavedPhotosAlbum(reconstructedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}


    - (void)image:(UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
        NSLog(@"SAVE IMAGE COMPLETE");
        if(error != nil) {
            NSLog(@"ERROR SAVING:%@",[error localizedDescription]);
        }
        [image autorelease];
    }
Dancreek
  • 9,524
  • 1
  • 31
  • 34
  • Not late at all. Actually, I had placed this project on hold 'cause I had an idea for another one, and just submitted that one to the AppStore. :) Gonna try this today, thanks. – EduAlm Jul 07 '10 at 11:51
0

I would start by adding a completion target method to the save call to see if it is producing any errors. See the UIImageWriteToSavedPhotosAlbum documentation for details.

Edit:

One of the times it returned... wait_fences: failed to receive reply: 10004003

That means the operation timed out. There are many causes of this:

  1. Low memory: put a log in didReceiveMemoryWarning to see if this is the cause.
  2. Corrupt files that the system can't save. If the image is generated each time it might not come out right each time.
  3. Improperly reported disk full error.

I think (1) most likely and explains the intermittent nature. When you have enough memory the method works and when you don't it fails.

In addition, when you compile, resolve any warnings that come up. Those are just errors that won't show up until runtime. You're problem might be listed there

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • One of the times it returned... wait_fences: failed to receive reply: 10004003 – EduAlm Jun 11 '10 at 18:18
  • A friend tested it on his 3GS and the same thing happened. My application is not memory consuming at all so I don't think that should be the cause. I will try placing that anyway to see if it gives any memory warning. (And no, there are not any compilation warnings.) – EduAlm Jun 12 '10 at 16:32
  • ***Don't assume about the memory use.*** Test it. One of the weaknesses of Objective-C is that errors can create massive memory leaks. If its not memory then the next most likely problem is a corrupt image. If you're taking it straight from the camera I don't know how that would happen. I would try saving a test image instead of the one returned by the image picker to try and isolate the problem. – TechZen Jun 12 '10 at 20:18
  • On one app run, I took 4 pictures, 3 saved correctly, and another one didn't and triggered a memory warning. On the second run... I took 7 pictures, 2 saved correctly, no errors, no memory warnings. – EduAlm Jun 12 '10 at 23:14
  • Well, it looks like you may have some memory issues. I would see what caused that and work from there. Fire up Instruments and see what it says. If all your doing is taking a picture and then immediately saving it, I don't think the problem is directly related images themselves. You could try disabling every part of the app but this one and see if you have the problem. If not, reenable parts until you encounter the error. – TechZen Jun 13 '10 at 00:14
  • You might check and post how you setup the image picker. Perhaps your accidentally recording video or something. – TechZen Jun 13 '10 at 00:14
  • The setup looks okay. I suggest testing without the overlay and transform. You've got an odd intermittent problem and you're most likely simply going to have to experiment to find the cause. I'm still betting on a memory problem. – TechZen Jun 15 '10 at 20:00