2015年最后来一帖,等着2个小时后下班,迎接新一年,扔掉两级分化现象严重的2015年啊!
今天分享的是拍照、调用相册的方法!
1.遵守协议
@interface MyMessageVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#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(@"无法打开相机"); } }
#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]; } }
#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; }
+ (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); }]; }