Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

cocoa touch - UIImageWriteToSavedPhotosAlbum working... sometimes

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...