Solved

xcode how to save an image

Posted on 2010-09-20
4
2,269 Views
Last Modified: 2012-05-10
how do you save an image after you take a picture? In my app i'm using an UIImagePickerController that takes the picture but i don't know how to save the image.
0
Comment
Question by:BTMExpert
  • 2
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33722320
If you need to save it in the album: UIImageWriteToSavedPhotosAlbum
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html

Another way:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/40279-save-uiimagepickercontroller-user-default.html

The simplest case: in the didFinish you can save the image:
UIImageWriteToSavedPhotosAlbum(image, nil,nil,nil);

But this way will look better:
UIImageWriteToSavedPhotosAlbum(reconstructedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
and [image autorelease]; in the callback.
As it's shown here:
http://stackoverflow.com/questions/3017681/uiimagewritetosavedphotosalbum-working-sometimes

Another way.
The first step is always to convert UIImage to NSData:
NSData *ImageData = UIImageJPEGRepresentation(img, 0.9);

Now, when it's already NSData, we can save it. In that thread people saved in the preferences:
[[NSUserDefaults standardUserDefaults] setObject:ImageData forKey:@"image"];
      
More info about the question:
http://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more

0
 

Author Comment

by:BTMExpert
ID: 33724869
that helped so much thank you.  One small problem now.  everytime i click the button to save my image, it saves which is great but when i try to do anything else after that, the app crashes.

- (IBAction)saveImage {
      UIImage *image = imageView.image;      

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image" message:@"Image" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
      [alert show];
      [alert release];
      
      UIImageWriteToSavedPhotosAlbum(image, 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];
}
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33724898
Maybe, because of that line:  [image autorelease];

try to cmment it. If it does not help, comment the alert (all lthree lines about the alert).
 
0
 

Author Comment

by:BTMExpert
ID: 33724911
i took the [image release]; out and it works now. Thanks a lot!!!!!!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now