Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Created February 28, 2013 08:21
Show Gist options
  • Select an option

  • Save lamprosg/5055149 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5055149 to your computer and use it in GitHub Desktop.
(iOS) - Image-Video Picker
//You have to include MediaPlayer and MobileCoreServices frameworks to the project
#import <UIKit/UIKit.h>
//Needed protocols
@interface BIDViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *takePictureButton;
- (IBAction)shootPictureOrVideo:(id)sender;
- (IBAction)selectExistingPictureOrVideo:(id)sender;
#import "BIDViewController.h"
//Include the framework headers
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/UTCoreTypes.h>
@interface BIDViewController ()
//Private properties
//For displaying video the user will take
@property (strong, nonatomic) MPMoviePlayerController *moviePlayerController;
//For keeping the last selected image or video
@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSURL *movieURL;
//The type of the last media selected (image or video)
@property (copy, nonatomic) NSString *lastChosenMediaType;
@end
@implementation BIDViewController
//Evey time the view is loaded into memory
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Hide, upon load, the "Take Photo" button if the device doesn't have a camera
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
self.takePictureButton.hidden = YES;
}
}
//Evey time the view is displayed
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self updateDisplay];
}
/*******************************************/
//Actions
- (IBAction)shootPictureOrVideo:(id)sender
{
[self pickMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}
- (IBAction)selectExistingPictureOrVideo:(id)sender
{
[self pickMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}
//Update the display
- (void)updateDisplay
{
//Check what the user selected
if ([self.lastChosenMediaType isEqual:(NSString *)kUTTypeImage])
{
self.imageView.image = self.image;
self.imageView.hidden = NO;
//Hide the movie player controller (it's an image)
self.moviePlayerController.view.hidden = YES;
}
else if ([self.lastChosenMediaType isEqual:(NSString *)kUTTypeMovie])
{
//If it's a movie
[self.moviePlayerController.view removeFromSuperview];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];
[self.moviePlayerController play];
UIView *movieView = self.moviePlayerController.view;
movieView.frame = self.imageView.frame;
movieView.clipsToBounds = YES;
[self.view addSubview:movieView];
self.imageView.hidden = YES;
}
}
//Both actions will call this method
-(void)pickMediaFromSource:(UIImagePickerControllerSourceType)sourceType
{
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
if ([UIImagePickerController isSourceTypeAvailable:sourceType] && [mediaTypes count] > 0)
{
//Get all media types to an array
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.mediaTypes = mediaTypes;
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:NULL];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing media"
message:@"Device doesn't support that media source"
delegate:nil
cancelButtonTitle:@"Fine.."
otherButtonTitles:nil];
[alert show];
}
}
//Shrink the image down to the size of the view in which we’re going to show it.
- (UIImage *)shrinkImage:(UIImage *)original toSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
[original drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *final = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return final;
}
/*******************************************/
//Delegate methods for the Image Picker Controller
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Check whether an image or video was chosen
self.lastChosenMediaType = info[UIImagePickerControllerMediaType];
//If it's an image shrink it to our view
if ([self.lastChosenMediaType isEqual:(NSString *)kUTTypeImage])
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
UIImage *shrunkenImage = [self shrinkImage:chosenImage toSize:self.imageView.bounds.size];
self.image = shrunkenImage;
}
else if ([self.lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) //If it's a movie get the URL
{
self.movieURL = info[UIImagePickerControllerMediaURL];
}
//dismiss the image/video picker
[picker dismissViewControllerAnimated:YES completion:NULL];
}
//If the picker is cancelled just dismiss it
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
/*
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
*/
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment