IOS-照相,相片选取
来源:互联网 发布:js中eval函数是干嘛的 编辑:程序博客网 时间:2024/04/29 06:54
初始化
UIImagePickerControllerSourceType sourceType; switch (buttonIndex) { case 0: sourceType = UIImagePickerControllerSourceTypeCamera; if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } break; default: sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; } NSString *mediaType = AVMediaTypeVideo; AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; if(authStatus ==AVAuthorizationStatusRestricted){//此应用程序没有被授权访问的照片数据。可能是家长控制权限。 }else if(authStatus == AVAuthorizationStatusDenied){// 用户已经明确否认了这一照片数据的应用程序访问 [self showAlertView:@"热波间需要访问你的相机" message:@"头像修改,热波间需要访问你的相机权限。点击“设置”前往系统设置允许热波间访问你的相机" confirm:^(id sender) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } cancel:^(id sender) { }]; return; } else if(authStatus == AVAuthorizationStatusAuthorized){//用户已授权应用访问照片数据. UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = sourceType; picker.mediaTypes = @[(NSString *) kUTTypeImage]; [self presentViewController:picker animated:YES completion:nil]; }else if(authStatus == AVAuthorizationStatusNotDetermined){// 用户尚未做出了选择这个应用程序的问候.一次一出现 [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {//弹出授权弹框 if(granted){//点击允许访问时调用 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = sourceType; picker.mediaTypes = @[(NSString *) kUTTypeImage]; [self presentViewController:picker animated:YES completion:nil]; } else { return ; } }]; }else { NSLog(@"Unknown authorization status"); }
Delegate
#pragma mark UIImagePickerController Delegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; if ([mediaType isEqualToString:@"public.image"]) { MBProgressHUD *mbProgressHud = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:mbProgressHud]; [mbProgressHud show:YES]; //[info objectForKey:@"UIImagePickerControllerEditedImage"]; NSString *tempDir = NSTemporaryDirectory (); NSString *tempFile = [NSString stringWithFormat:@"%@/avatar.png",tempDir]; UIImage *avatar = [info objectForKey:@"UIImagePickerControllerEditedImage"]; float scale = MAX(avatar.size.width/150,avatar.size.height/150); avatar = [UIImage imageWithCGImage:avatar.CGImage scale:scale orientation:UIImageOrientationUp]; [UIImagePNGRepresentation(avatar) writeToFile:tempFile atomically:YES]; __weak typeof(self) weakSelf = self; updateCurrUserModel *userModel = [[updateCurrUserModel alloc] init]; [userModel uploadDataWithFileUrl:tempFile params:nil success:^(id object) { __strong typeof(self) strongSelf = weakSelf; if (strongSelf) { /*成功返回数据*/ if (userModel.result == 0) { strongSelf.selectedImg = avatar; strongSelf.headImgView.image = avatar; strongSelf.userInfo.photo = userModel.data; float quality = .00001f; float blurred = .2f; NSData *imageData = UIImageJPEGRepresentation([strongSelf.headImgView image], quality); UIImage *blurredImage = [[UIImage imageWithData:imageData] blurredImage:blurred]; _backImageView.image = blurredImage; [UserInfoManager shareUserInfoManager].currentUserInfo.photo = userModel.data; NSError *error; [[NSFileManager defaultManager] removeItemAtPath:tempFile error:&error]; } } [mbProgressHud hide:YES]; } fail:^(id object) { /*失败返回数据*/ [mbProgressHud hide:YES]; [self showNoticeInWindow:@"网络连接失败,请重试" duration:1.5]; }]; } [picker dismissViewControllerAnimated:YES completion:nil];}
图片裁剪
获取到图片后反转
//压缩图片 - (UIImage *)image:(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; }
方法2
//裁剪图片 - (UIImage *)cutImage:(UIImage*)image { //压缩图片 CGSize newSize; CGImageRef imageRef = nil; if ((image.size.width / image.size.height) < (_headerView.bgImgView.size.width / _headerView.bgImgView.size.height)) { newSize.width = image.size.width; newSize.height = image.size.width * _headerView.bgImgView.size.height / _headerView.bgImgView.size.width; imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, fabs(image.size.height - newSize.height) / 2, newSize.width, newSize.height)); } else { newSize.height = image.size.height; newSize.width = image.size.height * _headerView.bgImgView.size.width / _headerView.bgImgView.size.height; imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(fabs(image.size.width - newSize.width) / 2, 0, newSize.width, newSize.height)); } return [UIImage imageWithCGImage:imageRef]; }
0 0
- IOS-照相,相片选取
- 通过UIImagePickerController完成照相和相片的选取
- iOS 8 照相或从相册选取、
- ios 选取相片后assets-library的转化
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS 实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS---头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像 (转)
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- Android照相以及从相册中选择相片
- Downloadable NLG systems
- 树莓派上php通过sudo执行root权限的程序
- 伪静态URLRewrite
- OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能
- java根据汉字获取拼音的方法
- IOS-照相,相片选取
- CountDownLatch的介绍和使用
- 图像运算平移,旋转,切变换
- Eclipse快捷键大全
- C#接口实现& is和as的含义与使用
- idea intellij对Spring进行单元测试
- SAP HANA CE function的介绍及使用
- kohana框架index.php执行步骤
- 设计模式之--简单工厂模式