• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An easy-to-use UIImagePickerController replacement for picking Images and Videos.

CRMediaPickerController

A easy-to-use UIImagePickerController replacement for picking Images and Videos.

Platform Version CI License


Overview

Picking, taking, or using the last photo or video made easy!

Features

  • Allows user to pick/capture 2 types of media (Photo and Video).
  • Picking options: Camera, Camera Roll, Photo Library and Last photo/video taken.
  • Delegate protocol available for more control.
  • Fully customizable with UIImagePickerController properties (Camera Overlay, Camera Device, Camera View Transform, allowsEditing, etc).
  • Uses Assets Library Framework, provides original ALAsset. (Easy to deal with).
  • Supports Portrait & Landscape Modes.
  • Native iOS UI (It uses UIImagePickerController under the hood).
  • Easy to apply in your project.

Installation

There are two options:

CocoaPods

  • Add the dependency to your Podfile:
platform :ios
pod 'CRMediaPickerController'
...
  • Run pod install to install the dependencies.

Source files

  • Just clone this repository or download it in zip-file.
  • Then you will find source files under CRMediaPickerController directory.
  • Copy them to your project.

Usage

CRMediaPickerController is created and displayed in a very similar manner to the UIImagePickerController. To use CRMediaPickerController you need create an instance, configure it, display it and implement the delegate methods.

An example of creating and displaying a CRMediaPickerController instance:

self.mediaPickerController = [[CRMediaPickerController alloc] init];
self.mediaPickerController.delegate = self;
self.mediaPickerController.mediaType = CRMediaPickerControllerMediaTypeImage;
self.mediaPickerController.sourceType = CRMediaPickerControllerSourceTypePhotoLibrary;
[self.mediaPickerController show];

Properties

  • Media Type

mediaType

The media type for the picking or create process. Supports Photo, Video or both.

self.mediaPickerController.mediaType = (CRMediaPickerControllerMediaTypeImage | CRMediaPickerControllerMediaTypeVideo);

Available options:

typedef NS_OPTIONS(NSInteger, CRMediaPickerControllerMediaType) {
    CRMediaPickerControllerMediaTypeImage = 1 << 0,
    CRMediaPickerControllerMediaTypeVideo = 1 << 1
};
  • Source Type

sourceType

The source for picking or create the media file. Multiple sources supported.

self.mediaPickerController.sourceType = (CRMediaPickerControllerSourceTypePhotoLibrary | CRMediaPickerControllerSourceTypeCamera | CRMediaPickerControllerSourceTypeLastPhotoTaken);

Available options:

typedef NS_OPTIONS(NSInteger, CRMediaPickerControllerSourceType) {
    CRMediaPickerControllerSourceTypePhotoLibrary       = 1 << 0,
    CRMediaPickerControllerSourceTypeCamera             = 1 << 1,
    CRMediaPickerControllerSourceTypeSavedPhotosAlbum   = 1 << 2,
    CRMediaPickerControllerSourceTypeLastPhotoTaken     = 1 << 3
};

If sourceType property has multiple sources, it presents a UIActionSheet with the multiple (and available) options to choose. Otherwise, the single sourceType type is shown.

More properties:

@property (nonatomic, assign) NSTimeInterval videoMaximumDuration;
@property (nonatomic, assign) UIImagePickerControllerQualityType videoQualityType;
@property (nonatomic) BOOL allowsEditing;
@property (nonatomic, assign) UIImagePickerControllerCameraCaptureMode cameraCaptureMode;
@property (nonatomic, assign) UIImagePickerControllerCameraDevice cameraDevice;
@property (nonatomic, assign) UIImagePickerControllerCameraFlashMode cameraFlashMode;
@property (nonatomic, retain) UIView *cameraOverlayView;
@property (nonatomic, assign) CGAffineTransform cameraViewTransform;
@property (nonatomic, assign) BOOL showsCameraControls;

Instance Methods

- (void)show;
- (void)dismiss;
- (BOOL)startVideoCapture;
- (void)stopVideoCapture;
- (void)takePicture;

Delegate Methods

A CRMediaPickerController instance will return the selected media file back to CRMediaPickerControllerDelegate. The delegate contains methods very similar to the UIImagePickerControllerDelegate. Instead of returning one dictionary representing a single image the controller sends back the original ALAsset object which are easy to deal with.

  • Finished Picking Asset

- CRMediaPickerController:didFinishPickingAsset:error:

Tells the delegate that the picking process is done and the media file is ready to use.

- (void)CRMediaPickerController:(CRMediaPickerController *)mediaPickerController didFinishPickingAsset:(ALAsset *)asset error:(NSError *)error;

Parameters:

mediaPickerController The CRMediaPickerController object providing this information.
asset The media file picked as ALAsset object.
error An error object that describes why the picking process has failed.
  • Cancelled

- CRMediaPickerControllerDidCancel:

Tells the delegate that the user cancelled the picking process.

- (void)CRMediaPickerControllerDidCancel:(CRMediaPickerController *)mediaPickerController;

Parameters:

mediaPickerController The CRMediaPickerController object providing this information.

CPDMediaPickerControllerDelegate example

#pragma mark - CPDMediaPickerControllerDelegate

- (void)CRMediaPickerController:(CRMediaPickerController *)mediaPickerController didFinishPickingAsset:(ALAsset *)asset error:(NSError *)error
{
    if (!error) {
        
        if (asset) {
            
            if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                
                ALAssetRepresentation *representation = asset.defaultRepresentation;
                UIImage *image = [UIImage imageWithCGImage:representation.fullScreenImage];
                self.imageView.image = image;
                
            } else if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
                
                self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:asset.defaultRepresentation.url];
                self.moviePlayer.movieSourceType = MPMediaTypeMovie;
                self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
                self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
                self.moviePlayer.repeatMode = MPMovieRepeatModeNone;
                self.moviePlayer.allowsAirPlay = NO;
                self.moviePlayer.shouldAutoplay = NO;
                
                self.moviePlayer.view.frame = self.videoView.bounds;
                self.moviePlayer.view.autoresizingMask = (UIViewAutoresizing)(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
                [self.videoView addSubview:self.moviePlayer.view];
                
                [self.moviePlayer prepareToPlay];
                
            }
            
        } else {
            NSLog(@"No media selected");
        }
        
    } else {
        NSLog(@"%@", error.localizedDescription);
    }
}

- (void)CRMediaPickerControllerDidCancel:(CRMediaPickerController *)mediaPickerController
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

Note: Using AssetsLibrary.framework will prompt users to grant access to their photos.

Examples

Photo only

self.mediaPickerController = [[CRMediaPickerController alloc] init];
self.mediaPickerController.delegate = self;
self.mediaPickerController.mediaType = CRMediaPickerControllerMediaTypeImage;
self.mediaPickerController.sourceType = CRMediaPickerControllerSourceTypePhotoLibrary | CRMediaPickerControllerSourceTypeCamera;
[self.mediaPickerController show];

Video Only

self.mediaPickerController = [[CRMediaPickerController alloc] init];
self.mediaPickerController.delegate = self;
self.mediaPickerController.mediaType = CRMediaPickerControllerMediaTypeVideo;
self.mediaPickerController.sourceType = CRMediaPickerControllerSourceTypeCamera;
self.mediaPickerController.allowsEditing = NO;
self.mediaPickerController.videoQualityType = UIImagePickerControllerQualityTypeMedium;
self.mediaPickerController.videoMaximumDuration = 60.0f;
[self.mediaPickerController show];

Both Photo or Video

self.mediaPickerController = [[CRMediaPickerController alloc] init];
self.mediaPickerController.delegate = self;
self.mediaPickerController.mediaType = CRMediaPickerControllerMediaTypeImage | CRMediaPickerControllerMediaTypeVideo;
self.mediaPickerController.sourceType = CRMediaPickerControllerSourceTypeSavedPhotosAlbum;
[self.mediaPickerController show];

Camera Overlay

self.mediaPickerController = [[CRMediaPickerController alloc] init];
self.mediaPickerController.delegate = self;
self.mediaPickerController.mediaType = CRMediaPickerControllerMediaTypeImage;
self.mediaPickerController.sourceType = CRMediaPickerControllerSourceTypeCamera;
self.mediaPickerController.allowsEditing = NO;
    
self.mediaPickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
self.mediaPickerController.cameraOverlayView = self.overlayView;
self.overlayView = nil;
    
[self.mediaPickerController show];

Video with specific properties

self.mediaPickerController = [[CRMediaPickerController alloc] init];
self.mediaPickerController.delegate = self;
self.mediaPickerController.mediaType = CRMediaPickerControllerMediaTypeVideo;
self.mediaPickerController.sourceType = CRMediaPickerControllerSourceTypePhotoLibrary | CRMediaPickerControllerSourceTypeCamera;
self.mediaPickerController.allowsEditing = NO;
self.mediaPickerController.videoQualityType = UIImagePickerControllerQualityTypeMedium;
self.mediaPickerController.videoMaximumDuration = 60.0f;
[self.mediaPickerController show];

Demo

See Example Xcode project for example usage.

Requirements

  • iOS 7.0 or higher, ARC is must.
  • AssetsLibrary.Framework
  • AVFoundation.Framework

TODO

  • Improve API
  • iPad support
  • Multiple selection?
  • Image cropping?

Contributing

Anyone who would like to contribute to the project is more than welcome.

  • Fork this repo
  • Make your changes
  • Submit a pull request

License

CRMediaPickerController is released under the MIT license. See LICENSE.

Contact

Christian Roman

http://chroman.me

[email protected]

@chroman

More Repositories

1

CRGradientNavigationBar

Custom UINavigationBar subclass which allows gradient coloured navigation bar on iOS 7.
Objective-C
914
star
2

CRMotionView

A custom photo viewer that implements device motion scrolling, inspired by Facebook Paper.
Objective-C
758
star
3

Doppio

An open source iOS app to find the nearest Starbucks store using NSURLSession, AFNetworking 2.0, Mantle and Starbucks private API.
Objective-C
553
star
4

CRPixellatedView

Custom UIView subclass with a pixellated animation inspired by Facebook's Slingshot app.
Objective-C
375
star
5

ANPR

License plate recognition for iOS using OpenCV & Tesseract OCR Engine
C++
252
star
6

CRMultiRowSelect

Custom UITableViewCell for iOS that supports multiple row selection
Objective-C
171
star
7

HeartBeats

Source code for iOS app to draw heart beats by reading color changes using the device flash led and CoreGraphics.
Objective-C
168
star
8

CRGradientLabel

Custom UILabel subclass with gradient coloured background, written in Swift.
Swift
50
star
9

repuve-api

Python
13
star
10

df-gtfs

Script para importar dataset de "df_gtfs" a PostgreSQL
13
star
11

Ecobici

Ecobici+ iOS app. Disponibilidad inteligente de Ecobici, ciclovias y rutas seguras. #hackdf
Objective-C
10
star
12

API-SCT

API para consumir datos de rutas a traves de SCT
Ruby
9
star
13

XReset

Xcode Plugin to Reset iOS Simulators Content and Settings.
Objective-C
7
star
14

congreso

iOS app for #app115 challenge which Mexican House of Representatives announced that they were planning to pay $9.3 million to have an app developed. This an open source version and was presented at Mexican Congress.
Objective-C
7
star
15

FaceMemesCV

Live face detection & memes with OpenCV
C++
5
star
16

Toggle

Google Chrome extension to toggle between .h and .m source files on GitHub.
JavaScript
4
star
17

memeAR

iPhone app that detects real time faces and add memes
Objective-C
4
star
18

meds

Meds application for Android
Java
3
star
19

TextureViewDemo

Android TextureView Demo like the new Youtube app.
Java
3
star
20

chroMVC

PHP MVC framework based on Sinatra
JavaScript
2
star
21

cpmx4-chat

CPMX4 Node.js Chat
CSS
1
star
22

Programming-challenges

Programming challenges that solve in my free time from the book: The programming contest training manual
Java
1
star