TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Mar 2015 |
Maintained by Wenchao Ding.
@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]
}
mediaInfo
NSDictionary contains both the editedImage
and the circularEditedImage
no matter what the editMode
is. This setting because some app just displays the circularImage but needs to save the rectangle image on device or the server- (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);
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")
pod install
from the Example directory first.FSMediaPicker.h
and FSMediaPicker.m
in FSMediaPicker-master/Pod/Classes to your project, make sure to check copy items if needed
#import "FSMediaPicker.h"
iOS 7.0
[email protected]
Wenchao Ding, [email protected]
FSMediaPicker is available under the MIT license. See the LICENSE file for more info.