iOS调用系统相册,相机上传头像的基本技巧

//头像点击事件

-(void)photoViewBtn{

    UIActionSheet* sheet = [[UIActionSheet alloc

                             ]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从手机相册里选择", @"拍照", nil];

    sheet.tag = 1001;

    [sheet showInView:self.view];

}

#pragma mark - UIActionSheetDelegate

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

    //头像

    if (actionSheet.tag == 1001) {

        if (0 == buttonIndex) {

            [self LocalPhoto];

        } else if (1 == buttonIndex) {

            [self takePhoto];

        }

    }

    

    

}


//打开本地相册

-(void)LocalPhoto

{

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

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    picker.delegate = self;

    //设置选择后的图片可被编辑

    picker.allowsEditing = YES;

  [self presentViewController:picker animated:YES completion:nil];

}


//开始拍照

-(void)takePhoto

{

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

    if ([UIImagePickerController isSourceTypeAvailable:sourceType])

    {

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

        picker.sourceType = sourceType;

        picker.delegate = self;

        //设置拍照后的图片可被编辑

        picker.allowsEditing = YES;

[self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];

    }else{

        UIAlertView *al = [[UIAlertView alloc] initWithTitle:nil message:@"模拟器中无法打开照相机,请在真机中使用" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];

        [al show];

    }

}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated


{

    for (UINavigationItem *item in navigationController.navigationBar.subviews) {

        

        if ([item isKindOfClass:[UIButton class]]&&([item.title isEqualToString:@"取消"]||[item.title isEqualToString:@"Cancel"]))

            

        {

            UIButton *button = (UIButton *)item;

            [button setTitle:@"取消" forState:UIControlStateNormal];

            [button setTintColor:[UIColor blackColor]];

        }

    }

}

//当选择一张图片后进入这里

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

{

    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

    

    //当选择的类型是图片

    if ([type isEqualToString:@"public.image"])

    {

        // 2.取得的图片

        UIImage *image = info[UIImagePickerControllerOriginalImage];

        

        

        NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

        

        UIImage *img = [UIImage imageWithData:imageData];

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];

        imgView.image = img;

        

        NSData *imgViewData = [self imageWithImage:imgView.image scaledToSize:CGSizeMake(90, 90)];

        

        UIImage *suolue = [UIImage imageWithData:imgViewData]       

        //上传头像接口在这里调用

        [picker dismissViewControllerAnimated:YES completion:nil];

    }

}

//将图片缩略到指定大小

- (NSData *)imageWithImage:(UIImage*)image

              scaledToSize:(CGSize)newSize;

{

    UIGraphicsBeginImageContext(newSize);

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return UIImageJPEGRepresentation(newImage, 0.5);

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES completion:nil];

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值