美文网首页
UIImagePickerController 的使用

UIImagePickerController 的使用

作者: 流沙3333 | 来源:发表于2017-03-10 14:55 被阅读22次

    #import <UIKit/UIKit.h>

    @interfaceYBPhotoSelectView :UIView

    @property(nonatomic,weak)id target;

    @end

    #import"YBPhotoSelectView.h"

    #import <Masonry.h>

    #import"GlobalDefine.h"

    @interfaceYBPhotoSelectView()

    @property(nonatomic,weak)UIImageView* photoView;

    @end

    @implementationYBPhotoSelectView

    - (instancetype)initWithFrame:(CGRect)frame {

    if(self= [super initWithFrame:frame])

    {

    [self commonInit];

    }

    return self;

    }

    - (void)commonInit {

    UIImageView* photoView

    = [[UIImageView alloc]init];

    photoView.image= [UIImage imageNamed:@"dog"];

    photoView.userInteractionEnabled=YES;

    [self addSubview:photoView];

    self.photoView= photoView;

    [photoView mas_makeConstraints:^(MASConstraintMaker*make) {

    make.edges.equalTo(self);

    }];

    UITapGestureRecognizer*

    tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickedPhoto)];

    [photoView addGestureRecognizer:tap];

    }

    - (void)onClickedPhoto {

    UIActionSheet*popMenu = [[UIActionSheetalloc] initWithTitle:@"选择照片" delegate:self

    cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册",nil];

    UIViewController* vc = (UIViewController*)self.target; 

    [popMenu showInView:vc.view];

    }

    #pragma mark - actionSheet delegate methods

    -(void)getMediaFromSource : (UIImagePickerControllerSourceType) sourceType

    {

    NSArray*mediaTypes =

    [UIImagePickerController availableMediaTypesForSourceType:sourceType];

    if([UIImagePickerController isSourceTypeAvailable:sourceType]

    && [mediaTypes count] >0)

    {

    UIImagePickerController*picker = [[UIImagePickerController alloc]init];

    NSMutableArray*types = [NSMutableArray array];

    for(NSString*type in mediaTypes){

    if([typecompare:@"public.movie"]!=NSOrderedSame){

    [types addObject:type];

    }

    }

    picker.mediaTypes= types;

    picker.delegate=self;

    picker.allowsEditing=YES;

    picker.sourceType= sourceType;

    [self.target presentViewController:pickeranimated:YES  completion:nil];

    }

    else

    {

    UIAlertView*alert = [[UIAlertViewalloc]init];

    [alert setTitle:NSLocalizedString(@"note_tishi",@"")];

    [alert setMessage:@"您的设备不支持该功能。"];

    [alert setDelegate:nil];

    [alert addButtonWithTitle:@"知道了"];

    [alert show];

    }

    }

    -(void) actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

    {

    NSString*title = [actionSheet buttonTitleAtIndex:buttonIndex];

    if([title is EqualToString:@"拍照"])

    {

    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];

    }

    elseif([title is EqualToString:@"相册"])

    {

    [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];

    }

    }

    #pragmamark - UIImagePickerController delegate methods


    -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info

    {

    UIImage*chosenImage =

    [info objectForKey:UIImagePickerControllerEditedImage];

    if(picker.sourceType==UIImagePickerControllerSourceTypeCamera){

    }

    self.photoView.image= chosenImage;

    [self.tar getdismissViewControllerAnimated:YEScompletion:nil];

    }

    - (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo

    {

    if(error !=NULL)

    {

    }

    else// No errors

    {

    NSLog(@"image successfully saved");

    }

    }

    -(void) imagePickerControllerDidCancel:(UIImagePickerController*)picker

    {

    [self.target dismissViewControllerAnimated:YEScompletion:nil];

    }

    @end

    相关文章

      网友评论

          本文标题:UIImagePickerController 的使用

          本文链接:https://www.haomeiwen.com/subject/hbangttx.html