Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • UIImagePickerController

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 443
    Comment on it

    Often it is required to integrate the iPhone's camera in the app to take pictures and photos to be used by the app. This is made possible by UIImagePickerController.

    The UIImagePickerController gives the programmer the capability to integrate the phone's camera to the app's interface and use it to take photos and pics. Using the delegate methods of the ImagePickerController makes the job of implementing and using it a lot easier.

    The required code to perform the function is as follows.

    The first step is to integrate the phone's camera into the app's user interface.

    - (IBAction) useCamera: (id)sender
    {
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePicker =
            [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType =
            UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                      (NSString *) kUTTypeImage,
                                      nil];
            imagePicker.allowsEditing = NO;
            [self presentModalViewController:imagePicker
                                    animated:YES completion:nil];
            newMedia = YES;
        }
    }
    
    
    #Pragma mark ImagePickerController delegates
    
    -(void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
       NSString *mediaType = [info
               UIImagePickerControllerMediaType];
        [self dismissViewControllerAnimated:YES completion:nil];
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
            UIImage *image = [info
                objectForKey:UIImagePickerControllerOriginalImage];
    
            imageView.image = image;
            if (newMedia)
                UIImageWriteToSavedPhotosAlbum(image,
                   self,  
                   @selector(image:finishedSavingWithError:contextInfo:),
                   nil);
        }
        else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
        {
                    // Code here to support video if enabled
            }
    }
    -(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];
    
        }
    }
    

    PS. Don't forget to include the UIImagePickerControllerDelegate in the .h file of your ViewController.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: