UIImagePickerController使用

// 生成一个UIImagePickerController
- (UIImagePickerController *)pickerController
{
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    // 设置导航栏的背景⾊
    pickerController.navigationBar.tintColor = [UIColor whiteColor];
    pickerController.navigationBar.translucent = NO;
    [pickerController.navigationBar setBackgroundImage:[UIImage imageWithColor:ICAN_BLACK_COLOR] forBarMetrics:UIBarMetricsDefault];
    // 设置导航栏字体颜色
    NSDictionary *dict = @{ NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : ICAN_FONT(18)};
    pickerController.navigationBar.titleTextAttributes = dict;
    pickerController.navigationBar.barStyle = UIBarStyleDefault;
    // 设置代理
    pickerController.delegate = self;
    
    return pickerController;
}


#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    UIImage *yourImage = image;
    float compressionQuality = 0.6;//60 percent compression
    CGRect rect = CGRectMake(0.0, 0.0, 256, 256);
    UIGraphicsBeginImageContext(rect.size);
    [yourImage drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
    
    // 上传头像
}

/*If Don't pick any image*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - 照相相关,点击照片或者照相
/*Open Camera*/
- (void)takePhoto {
    
    UIImagePickerController *picker = [self pickerController];
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:nil];
}

/*Select Photo*/
- (void)selectPhoto {
    
    UIImagePickerController *picker = [self pickerController];
    //    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:picker animated:YES completion:nil];
}

// 修改UIImagePickerController导航栏文字为中文
// 在Info.plist中修改Key:Localization native development region,value = China即可



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值