UIImageWriteToSavedPhotosAlbum working… sometimes

蓝咒 提交于 2019-11-27 15:26:22

问题


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

回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/3017681/uiimagewritetosavedphotosalbum-working-sometimes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!