All I want to do is after the alert is shown that the picture was or wasn't saved the picker should be dismissed, but I can't manage to find out how.
I present the image control like this :
[self presentModalViewController:imagePicker animated:YES];
I know i need to dismissmodalviewcontroller, because i do it for the cancel case, but I don't know where to place it so that after the image is saves it exists.
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
You will have to create the instance of UIImagePickerController locally and in alertview delegate method, simply dismiss it.
it is depends on how you present imagepickercontroller if you have used peserntmodelviewcontrolleranimated: then use dismissModalViewControllerAnimated:
Related
I'm using UIImagePickerController to take picuture and save to photo gallery. When i launch the picker it has the button to photo take and cancel after taking photo it shows 2 button Retake & use, if i use, use button then only image saving to photo album but after saving i can't go to previous page or close the picker.
-(void)takepicture:(id)sender{
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentModalViewController:imagePicker animated:YES];
// [self performSelector:#selector(onTimer_Loadpicture:) withObject:nil afterDelay:0.5];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[picker release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
[self performSelector:#selector(onTimer_Loadpicture:) withObject:nil afterDelay:0.5];
}
just dismiss your view controller. like this ,update your didFinishPickingMediaWithInfo method
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
You are not dismissing your view controller.You should do like this and if part of this code is not necessary it is up to you that you want to check your image in imageView or not.If you do not want to check then simply remove the if part and use else part's code.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo{
[self dismissModalViewControllerAnimated:YES];
_imgview.image = [editingInfo valueForKey:#"UIImagePickerControllerOriginalImage"];
if(_imgview==nil){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Angry!!!" message:#"Vennligst velg et bilde!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
else{
_imgview.image = img;
imageData=[self compressImage:img];
[picker dismissModalViewControllerAnimated:YES];
_lblname.hidden=true;
}
return;
}
I'm trying to capture an image or video using the camera in iOS and add some text to a black bar overlayed at the bottom of the image.
I'm trying to get the text from a user input and save the image with the text and rectangle at the bottom of the image:
How would I go about this?
I currently have this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
// A photo was taken/selected!
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
// Save the image!
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
}
}
else {
// A video was taken/selected!
self.videoFilePath = (__bridge NSString *)([[info objectForKey:UIImagePickerControllerMediaURL] path]);
if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
// Save the video!
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(self.videoFilePath, nil, nil, nil);
}
}
}
[self dismissViewControllerAnimated:YES completion:nil]; }
What would be the best way of going about this?
You can add a UIView over the UIImageView with black (with alpha to be 0.5) background and on this UIView you can add a UILabel with your text.
I'd say have the user do an input using a UIAlertView
here's a sample
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Title Here"
message:#"message"
delegate:self
cancelButtonTitle:#"Cancel" // or nil
otherButtonTitles:#"Ok",
nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
that shows that an alert will appear with a text box in it. Since we set self to delegate, you have to call that delegate as such
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Ok"]) {
NSString *alertText = [alertView textFieldAtIndex:0].text;
// create UILabel here and set the text as advised in answer above
// example with global UILabel *overlayLabel;
self.overlayLabel.text = alertText;
// self.overlayLabel.font = [UIFont ...];
}
}
This is essentially what apple does. Go to the music app, and hit the "new playlist" and you'll see it in action
hope this helps!
I'm using the following code to save a screenshot from a Cocos2d layer to the camera roll..
-(void) takeScreenshot {
[self turnAllNotesBlack];
CCScene *scene = [[CCDirector sharedDirector] runningScene];
CCNode *n = [scene.children objectAtIndex:0];
UIImage *img = [self screenshotWithStartNode:n];
//NSParameterAssert(img);
UIImageWriteToSavedPhotosAlbum(img,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
[self resetIlluminatedNotes];
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Save failed"
message: #"Failed to save image"
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
It works fine on the simulator, but when I run it on my phone, it calls the error method, and no picture is saved.
Does anyone know why this is?
Carl
Ahh the answer lies in the phone itself..
I had to go to 'Settings -> Privacy -> Photos' and enable photos for my new App..
I have been trying this for the past 2 days and I am not able to figure out the answer. I have searched all over and I haven't found the answer.
The question is I have a button which brings up the camera in my app(to take photos only). The camera opens up, but when i take a picture and click on "USE"(which is displayed at the bottom right) its crashing. Also, when the camera opens up, before taking a picture when I click "Cancel" it again crashes.
I tried using breakpoints and found out that, When I click on the "USE" button, it crashes in this line
[picker dismissViewControllerAnimated:YES completion:Nil]
I'm testing it in my iPad (iOS6).
Here is the Button Code here :
-(IBAction)getAlbum:(id)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
//[self presentModalViewController:picker animated:YES]; //Since [Modal](http://stackoverflow.com/questions/12445190/dismissmodalviewcontrolleranimated-deprecated) has been removed
[self presentViewController:picker animated:YES completion:Nil];
//[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unsupported!"
message:#"Camera does not support photo capturing."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"This device does not have a camera."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
imagePickerController Method here:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Media Info: %#", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
selectedLogoImg.image=photoTaken; //selectedLogoImg is the imageView
[self.clipartItemView addSubview:selectedLogoImg]; // To detect touch and move it I place it as a subview of self.clipartItemView
}
//[picker dismissModalViewControllerAnimated:YES];
[picker dismissViewControllerAnimated:YES completion:Nil]
//[picker release];
//[picker dismissViewControllerAnimated:YES completion:^{
// NSLog(#"Dismiss completed");
//}];
}
didFinishSavingWithError Code Here:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
UIAlertView *alert;
//NSLog(#"Image:%#", image);
if (error) {
alert = [[UIAlertView alloc] initWithTitle:#"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
imagePickerControllerDidCancel Code Here:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//[picker dismissModalViewControllerAnimated:YES];
/*[picker dismissViewControllerAnimated:YES completion:^{
[self.view sendSubviewToBack:cardGalleryView];
}];*/
[picker dismissViewControllerAnimated:YES completion:Nil];
}
You should send the dismissViewControllerAnimated:completion: message to the view controller, not the picker. Try:
[self dismissViewControllerAnimated:YES completion:nil];
The above method is only for iOS 6. You need to use [self dismissModalViewControllerAnimated:YES] for iOS 5 and below.
Take a look at the description of the method in the documentation:
Dismisses the view controller that was presented by the receiver.
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, however, it automatically forwards the message to
the presenting view controller
This problem is due to your UIImagePickerController *picker object . ViewController isn't able to identify your picker object reference out of the getAlbum method scope.
1.> you can create UIImagePickerController object in your .h file
#interface yourViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate>
{
UIImagePickerController *picker;
UIPopoverController *popover;
}
and in .m file you just use it inside getAlbum IBAction method
-(IBAction)getAlbum:(id)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIButton *btn=(UIButton *)sender;
if ([popover isPopoverVisible])
{
[popover dismissPopoverAnimated:YES];
popover=nil;
}
picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:CGRectMake(btn.frame.size.width,btn.frame.size.height/2,1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unsupported!"
message:#"Camera does not support photo capturing."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"This device does not have a camera."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)myimage editingInfo:(NSDictionary *)editingInfo
{
myimage = [myimage fixOrientation];
[picker dismissModalViewControllerAnimated:YES];
[popover dismissPopoverAnimated:YES];
}
I hope it helps you to better understand.
I have my current image that can swipe back and forward. All the image are inside a folder called pages. So when I am in the current view, I have an IBAction to save that current image to photo album. How to implement that task ? I have study this but I don't know how to start and i'm stuck.
M file :
I define UIImage.h
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
);
- (IBAction)saveMe{
[UIImageWriteToSavedPhotosAlbum];
}
My Amendment code:
-(IBAction) saveToPhotoAlbum{
}
- (void) imageViewController:(UIImageView *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = //i have 6 current UIImageview on my current main view. How do I get stated?
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker release];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
draw uiview to context
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
and use UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); for save to album