Skip to content

An integration of UIActionSheet, UIAlertController and UIImagePickerController which allows you to pick media(image/video) effortlessly, with circular crop supported, and written in Objective-C

License

Notifications You must be signed in to change notification settings

WenchaoD/FSMediaPicker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FSMediaPicker

Version License Platform

fsmediapicker-edit               fsmediapicker-circular-edited

Purpose

How would we pick a image from device before

@interface OriginalViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)pickButtonClicked:(id)sender;

@end

@implementation OriginalViewController

- (void)pickButtonClicked:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                            delegate:self 
                                                            cancelButtonTitle:@"Cancel" 
                                                            destructiveButtonTitle:nil 
                                                            otherButtonTitles:@"Take photo",
                                                                              @"Select photo from camera", nil];
    actionSheet.cancelButtonIndex = 2;
    [actionSheet showInView:sender];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        return;
    };
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePicker.allowsEditing = YES;
    if (buttonIndex == 0 && [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else if (buttonIndex == 1 && [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    [self presentViewController:imagePicker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.imageView.image = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end

Why would I write such a long page just for a tiny little image?! why

With FSMediaPicker

@interface MediaPickerController : UIViewController <FSMediaPickerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)pickButtonClicked:(id)sender;

@end

@implementation MediaPickerController

- (void)pickButtonClicked:(id)sender
{
    FSMediaPicker *picker = [[FSMediaPicker alloc] init];
    picker.delegate = self;
    [picker showFromView: sender];
}

- (void)mediaPicker:(FSMediaPicker *)mediaPicker didFinishWithMediaInfo:(NSDictionary *)mediaInfo
{
    self.imageView.image = mediaInfo.editedImage; // Or mediaInfo[UIImagePickerControllerEditedImage]
}

@end

FSMediaPicker can handle the all borthering part for you

What's more

To Pick a video

mediaPicker.type = FSMediaTypeVideo // default is FSMediaTypePhoto

- (void)mediaPicker:(FSMediaPicker *)mediaPicker didFinishWithMediaInfo:(NSDictionary *)mediaInfo
{
    self.videoURL = mediaInfo.mediaURL; // Or mediaInfo[UIImagePickerControllerMediaURL]
}

fsmediapicker-type-video

To pick both image && video:

mediaPicker.mediaType = FSMediaTypeAll

- (void)mediaPicker:(FSMediaPicker *)mediaPicker didFinishWithMediaInfo:(NSDictionary *)mediaInfo
    if (mediaInfo.mediaType == FSMediaTypeVideo) { // Or [mediaInfo[UIImagePickerControllerMediaType] isEqualToString:(NSString)kUTTypeMovie];
        NSURL *url = mediaInfo.mediaURL; // Or mediaInfo[UIImagePickerControllerMediaURL]
    } else if (mediaInfo.mediaType == FSMediaTypePhoto) { // Or [mediaInfo[UIImagePickerControllerMediaType] isEqualToString:(NSString)kUTTypeImage];
        UIImage *someImage = mediaInfo.editedImage // Or mediaInfo[UIImagePickerControllerEditedImage]
    }
}

fsmediapicker-type-all

Some spices

Circular crop

mediaPicker.editMode = FSEditModeCircular; // defualt is FSEditModeStandard

- (void)mediaPicker:(FSMediaPicker *)mediaPicker didFinishWithMediaInfo:(NSDictionary *)mediaInfo
{
    imageView.image = mediaInfo.circularEditedImage; // Or mediaInfo[UIImagePickerControllerCircularEditedImage]
}
  • The mediaInfo NSDictionary contains both the editedImage and the circularEditedImage no matter what the editMode is. The reason for this setting it that some apps just display the circularImage but needs to save the rectangle image on device or the server, or some apps prefer to display the circular image in other ways, such as adding mask layer, or setting cornerRadius

Customized setting of UIImagePickerController:

- (void)mediaPicker:(FSMediaPicker *)mediaPicker willPresentImagePickerController:(UIImagePickerController *)imagePicker
{
    if ([imagePicker.sourceType isEqualToString:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.showsCameraControl = NO;
        imagePicker.cameraOverlayView = yourCustomOverlayView;
    }
}

When cancel

- (void)mediaPickerDidCancel:(FSMediaPicker *)mediaPicker
{
    // do something
}

This delegate method will be called on either when the UIActionSheet cancel or when the UIImagePickerController cancel

Block support

@property (copy, nonatomic) void(^willPresentImagePickerBlock)(FSMediaPicker *mediaPicker, UIImagePickerController *imagePicker);
@property (copy, nonatomic) void(^finishBlock)(FSMediaPicker *mediaPicker, NSDictionary *mediaInfo);
@property (copy, nonatomic) void(^cancelBlock)(FSMediaPicker *mediaPicker);

中文选项

  • 如果系统语言设置为中文并且Project下的Localization下包含Chinese,则选项中的文字显示为中文

localization fsmediapicker-chinese

  • 如果觉得文字不够高大上,在FSMediaPicker.m中开头的几个macro中设置对应文字.
// change the option string here
#define kTakePhotoString LocalizedString(@"Take photo")
#define kSelectPhotoFromLibraryString LocalizedString(@"Select photo from photo library")
#define kRecordVideoString LocalizedString(@"Record video")
#define kSelectVideoFromLibraryString LocalizedString(@"Select video from photo library")
#define kCancelString LocalizedString(@"Cancel")

Install

set up

  • cocoapods: To run the example project, clone the repo, and run pod install from the Example directory first.
  • manually: drag FSMediaPicker.h and FSMediaPicker.m in FSMediaPicker-master/Pod/Classes to your project, make sure to check copy items if needed

then

#import "FSMediaPicker.h"

Requirements

iOS 7.0

Contribution

Author

Wenchao Ding, [email protected]

License

FSMediaPicker is available under the MIT license. See the LICENSE file for more info.

About

An integration of UIActionSheet, UIAlertController and UIImagePickerController which allows you to pick media(image/video) effortlessly, with circular crop supported, and written in Objective-C

Resources

License

Stars

Watchers

Forks

Packages

No packages published