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];
}
#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]; // 跳到相册
}
#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);
}];
}