UIImagePickerController 调用摄像头和相册
按钮点击调用
//拍照按钮 UIButton *button5 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button5.frame = CGRectMake(50, 10, 220, 40); [button5 setTitle:@"拍照" forState:UIControlStateNormal]; [button5 addTarget:self action:@selector(pickPhotoFromCamera:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button5]; h += 60 +interval; //本地照片按钮 UIButton *button6 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button6.frame = CGRectMake(50, 60, 220, 40); [button6 setTitle:@"相册" forState:UIControlStateNormal]; [button6 addTarget:self action:@selector(pickPhotoFromAlbum:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button6];
//选择拍照 -(void)pickPhotoFromCamera:sender{ _imagePickerController = [[UIImagePickerController alloc] init]; _imagePickerController.delegate = self; _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; _imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; _imagePickerController.allowsEditing = YES; [self presentModalViewController:_imagePickerController animated:YES]; } //选择相册 -(void)pickPhotoFromAlbum:sender{ _imagePickerController = [[UIImagePickerController alloc] init]; _imagePickerController.delegate = self; _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; _imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; _imagePickerController.allowsEditing = YES; [self presentModalViewController:_imagePickerController animated:YES]; }
UIImagePickerControllerDelegate Methods
#pragma mark UIImagePickerControllerDelegate Methods -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; BOOL success; //文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; //当选择的类型是图片 if ([mediaType isEqualToString:@"public.image"]){ UIImage *image1 = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; image1 = [UIImagefixOrientation fixOrientation:image1]; //先把图片转成NSData NSData *data; if (UIImagePNGRepresentation(image1) == nil){ data = UIImageJPEGRepresentation(image1, 1.0); }else{ data = UIImagePNGRepresentation(image1); } NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; // 将double转为long long型 long long dTime = [[NSNumber numberWithDouble:time] longLongValue]; // 输出long long型 NSString *photoName = [NSString stringWithFormat:@"%llu.png",dTime]; //图片保存的路径 //这里将图片放在沙盒的documents文件夹中 //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png [fileManager createDirectoryAtPath:[GlobalSingleton sharedSingleton].documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil]; [fileManager createFileAtPath:[[GlobalSingleton sharedSingleton].documentsDirectory stringByAppendingPathComponent:photoName] contents:data attributes:nil]; [picker dismissModalViewControllerAnimated:YES]; //以下是自定义后续处理 _photoPath = photoName; _imageView.image = image1; //解决图片旋转90度显示的问题 _imageView.frame = [UIImagefixOrientation makeRectWithImageWidth:image1.size.width imageHeight:image1.size.height viewWidth:_imageView.frame.size.width viewHeight:_imageView.frame.size.height positionX:_imageView.frame.origin.x positionY:_imageView.frame.origin.y]; }else if([mediaType isEqualToString:@"public.movie"]){//选择视频 NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; NSLog(@"%@",videoURL); NSLog(@"found a video"); NSData *videoData = [NSData dataWithContentsOfURL:videoURL]; NSString *videoFile = [[GlobalSingleton sharedSingleton].documentsDirectory stringByAppendingPathComponent:@"temp.mov"]; NSLog(@"%@",videoFile); success = [fileManager fileExistsAtPath:videoFile]; if(success) { success = [fileManager removeItemAtPath:videoFile error:&error]; } [videoData writeToFile:videoFile atomically:YES]; } [_imagePickerController dismissModalViewControllerAnimated:YES]; } //取消选择 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [_imagePickerController dismissModalViewControllerAnimated:YES]; }
//解决图片旋转90度显示的方法
/** 拍照时旋转90度,保存时调整回来 */ +(UIImage *)fixOrientation:(UIImage *)aImage { // No-op if the orientation is already correct if (aImage.imageOrientation == UIImageOrientationUp) return aImage; // We need to calculate the proper transformation to make the image upright. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. CGAffineTransform transform = CGAffineTransformIdentity; switch (aImage.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, aImage.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; default: break; } switch (aImage.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); break; default: break; } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height, CGImageGetBitsPerComponent(aImage.CGImage), 0, CGImageGetColorSpace(aImage.CGImage), CGImageGetBitmapInfo(aImage.CGImage)); CGContextConcatCTM(ctx, transform); switch (aImage.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage); break; } // And now we just create a new UIImage from the drawing context CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img; }