码迷,mamicode.com
首页 > 其他好文 > 详细

文章标题

时间:2016-05-13 04:27:27      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

“`
//背景点击方法
- (void)bgAction{

UIActionSheet *bgActionImage = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];
bgActionImage.tag = 521;
[bgActionImage showInView:self.view];

}
//头像点击方法
- (void)userAction{

UIActionSheet *headActionImage = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];
headActionImage.tag = 520;
[headActionImage showInView:self.view];

}

pragma mark - 更换背景图片和头像

  • (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    if (actionSheet.tag == 520) {
    NSLog(@”头像”);
    _isPortrait = YES;
    [self getImage:buttonIndex];
    }else if(actionSheet.tag == 521){
    NSLog(@”背景图片”);
    _isPortrait = NO;
    [self getImage:buttonIndex];
    }
    }

  • (void)getImage:(NSInteger)buttonIndex
    {

    //相册获取
    if (buttonIndex == 0) {

    UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    picker.sourceType =     UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
    

    }else if (buttonIndex == 1){
    UIImagePickerControllerSourceType sourceType= UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = sourceType;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
    }else{
    NSLog(@”模拟器打不开拍照功能,请在真机中使用”);
    }

    }
    }

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
NSLog(@”isPortrait === %d”,_isPortrait);
if (_isPortrait) {

        [self uploadPhoto:reviseHeadImage_API photoImage:image];

    }else{
        [self uploadPhoto:reviseBackImage_API photoImage:image];
    }
}

}

  • (void)uploadPhoto:(NSString )photoUrl photoImage:(UIImage )image
    {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *dataDic = [userdefault objectForKey:@”rmxjyData”];
    NSLog(@”photo====%@”,photoUrl);
    [manager POST:[NSString stringWithFormat:@”%@%@”,BaseUrl_API,photoUrl] parameters:[@{
    @”key”:dataDic[@”key”],
    @”userId”:dataDic[@”userId”]
    }mutableCopy] constructingBodyWithBlock:^(id formData) {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置时间格式
    formatter.dateFormat = @”yyyyMMddHHmmss”;
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@”%@.png”, str];
    NSData *headData = UIImageJPEGRepresentation(image, .5);
    [formData appendPartWithFileData:headData name:@”photo” fileName:fileName mimeType:@”image/png”];
    NSLog(@”%@”,formData);
    } success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@”%@”,responseObject);
    if ([responseObject[@”code”] intValue] == 0) {

        [self.personModel getData:^(NSMutableArray *array) {
    
            if (_isPortrait) {
    
                [_userButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ImgPath_API,self.personModel.userImage]]]] forState:UIControlStateNormal];
            }else{
    
                [_bgButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ImgPath_API,self.personModel.userBackimage]]]] forState:UIControlStateNormal];
            }
            NSLog(@"userImage ====%@",self.personModel.userImage);
        }];
    }
    [hud hide:YES];
    

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@”%@”,error);
    [hud hide:YES];
    }

}

要想使相册里面的英文变为中文在info.plist文件里面设置如下两个参数
info.plist里面添加
Localizedresources can be mixed YES
Localization native development region China
添加完就OK了
技术分享技术分享

文章标题

标签:

原文地址:http://blog.csdn.net/g_treasure/article/details/51336278

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!