CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.
TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jan 2015 |
Maintained by Christian Roman.
A easy-to-use UIImagePickerController
replacement for picking Images and Videos.
Picking, taking, or using the last photo or video made easy!
UIImagePickerController
properties (Camera Overlay, Camera Device, Camera View Transform, allowsEditing, etc).ALAsset
. (Easy to deal with).UIImagePickerController
under the hood).There are two options:
CocoaPods
platform :ios
pod 'CRMediaPickerController'
...
pod install
to install the dependencies.Source files
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];
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
};
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;
- (void)show;
- (void)dismiss;
- (BOOL)startVideoCapture;
- (void)stopVideoCapture;
- (void)takePicture;
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.
- 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. |
- CRMediaPickerControllerDidCancel:
Tells the delegate that the user cancelled the picking process.
- (void)CRMediaPickerControllerDidCancel:(CRMediaPickerController *)mediaPickerController;
Parameters:
mediaPickerController | The CRMediaPickerController object providing this information. |
#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.
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];
See Example Xcode project for example usage.
Anyone who would like to contribute to the project is more than welcome.
CRMediaPickerController
is released under the MIT license. See
LICENSE.
Christian Roman