iOS - 调用相机相册的方法 + 上传图片



2015年最后来一帖,等着2个小时后下班,迎接新一年,扔掉两级分化现象严重的2015年啊!


今天分享的是拍照、调用相册的方法!


1.遵守协议

@interface MyMessageVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

2.写两个按钮  一个是拍照按钮  一个是打开相册按钮

#pragma mark - 点击拍照
- (IBAction)takePhotoClick:(id)sender {
    
    [self openCamera];
    

    
}

#pragma mark - 从相册选择按钮点击
- (IBAction)albumBtnClick:(id)sender {
    
    [self LocalPhoto];
    
    
}



3.点击拍照按钮 实现openCamara方法

#pragma mark - 打开相机
-(void)openCamera{
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;  //相册代理
        picker.allowsEditing = YES;  //允许相片编辑
        picker.sourceType = sourceType;  
        [self presentViewController:picker animated:YES completion:nil];  //跳转相机
    } else {
        NSLog(@"无法打开相机");
        
        
    }
}

4.点击打开相册按钮  实现LocalPhoto方法

#pragma mark - 从相册选择照片
-(void)LocalPhoto{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 内容类型
    picker.delegate = self; // 设置代理
    picker.allowsEditing = YES; // 是否可以编辑
    [self presentViewController:picker animated:YES completion:nil]; // 跳到相册
}


5.实现相片代理方法

#pragma mark - 相片代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    // 获取文件类型
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    // 判断是不是图片
    if ([type isEqualToString:@"public.image"]) {
        
        // 编辑后的图片
        self.EditedImage = info[@"UIImagePickerControllerEditedImage"];
        //显示所选图片
        self.headImageview.image = self.EditedImage;
        
        
        //******************************  上传图片至网络 ***********************
        
        
        // 关闭相册
        [picker dismissViewControllerAnimated:YES completion:nil];
        

    }
}


6.补充一个压缩图片的方法 压缩图片的作用目的是上传图片的时候 不至于数据流很大 上传缓慢

#pragma mark - 压缩图片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}


7.再补充一下AFN上传图片的方法 此处AFN为3.0版本

+ (void )uploadImage:(UIImage *)image withBlock:(void(^)(NSDictionary * dict))block andFailure:(void(^)(void))failure{
    
    NSData * imageData;
    
    
    //判断图片是不是png格式的文件
    if (UIImagePNGRepresentation(image)) {
        //返回为png图像。
        imageData = UIImagePNGRepresentation(image);
        
    }else {
        
        //返回为JPEG图像。
        imageData = UIImageJPEGRepresentation(image, 1.0);
    }
    
    NSLog(@"%@",imageData);
    
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.requestSerializer.timeoutInterval = 10;
    mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [mgr POST:uploadImageURL parameters:@{@"token":userToken} constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        if (imageData != nil) {
            
            
            
            //判断图片是不是png格式的文件
            if (UIImagePNGRepresentation(image)) {
                //返回为png图像。
                [formData appendPartWithFileData:imageData name:@"photo" fileName:@"photo.png" mimeType:@"image/png"];
                
            }else{
                
                //返回为JPEG图像。
                [formData appendPartWithFileData:imageData name:@"photo" fileName:@"photo.jpeg" mimeType:@"image/jpeg"];
            }
            
            
        }
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    
        NSString * str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        block(dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
         NSLog(@"错误%@",error);
        NSDictionary * dic = @{@"error":error};
        block(dic);
        
    }];
    
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值