Solved

xcode how to save an image

Posted on 2010-09-20
4
2,281 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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