@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?!
@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
mediaPicker.type = FSMediaTypeVideo // default is FSMediaTypePhoto
- (void)mediaPicker:(FSMediaPicker *)mediaPicker didFinishWithMediaInfo:(NSDictionary *)mediaInfo
{
self.videoURL = mediaInfo.mediaURL; // Or mediaInfo[UIImagePickerControllerMediaURL]
}
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]
}
}
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 theeditedImage
and thecircularEditedImage
no matter what theeditMode
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
- (void)mediaPicker:(FSMediaPicker *)mediaPicker willPresentImagePickerController:(UIImagePickerController *)imagePicker
{
if ([imagePicker.sourceType isEqualToString:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.showsCameraControl = NO;
imagePicker.cameraOverlayView = yourCustomOverlayView;
}
}
- (void)mediaPickerDidCancel:(FSMediaPicker *)mediaPicker
{
// do something
}
This delegate method will be called on either when the UIActionSheet cancel or when the UIImagePickerController cancel
@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
,则选项中的文字显示为中文
- 如果觉得文字不够高大上,在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")
- cocoapods: To run the example project, clone the repo, and run
pod install
from the Example directory first. - manually: drag
FSMediaPicker.h
andFSMediaPicker.m
in FSMediaPicker-master/Pod/Classes to your project, make sure to checkcopy items if needed
#import "FSMediaPicker.h"
iOS 7.0
- Issue me, or send it to
[email protected]
- Fork and send pull request
Wenchao Ding, [email protected]
FSMediaPicker is available under the MIT license. See the LICENSE file for more info.