#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/UTCoreTypes.h>
@interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, weak) IBOutlet UIImageView *imageView;
@property (nonatomic, weak) IBOutlet UIButton *takePicButton;
@property (nonatomic, strong) MPMoviePlayerViewController *moviePlayController;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSURL *movieUrl;
@property (nonatomic, copy) NSString *lastChooseMediaType;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
// self.takePicButton.hidden = YES;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self updateDisplay];
}
- (void)updateDisplay {
if ([self.lastChooseMediaType isEqual:(NSString *)kUTTypeImage]) {
self.imageView.image = self.image;
self.imageView.hidden = NO;
self.moviePlayController.view.hidden = YES;
} else if ([self.lastChooseMediaType isEqual:(NSString *)kUTTypeMovie]) {
[self.moviePlayController.view removeFromSuperview];
self.moviePlayController = [[MPMoviePlayerViewController alloc] initWithContentURL:self.movieUrl];
UIView *movieView = self.moviePlayController.view;
movieView.frame = self.imageView.frame;
movieView.clipsToBounds = YES;
[self.view addSubview:movieView];
self.imageView.hidden = YES;
}
}
- (void)pickMediaFromSource:(UIImagePickerControllerSourceType)sourceType {
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
if ([UIImagePickerController availableMediaTypesForSourceType:sourceType] && [mediaTypes count] > 0) {
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.mediaTypes = mediaTypes;
pickerController.delegate = self;
pickerController.editing = YES;
pickerController.sourceType = sourceType;
[self presentViewController:pickerController animated:YES completion:nil];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Erroe" message:@"Unknown media source" delegate:nil cancelButtonTitle:@"drat" otherButtonTitles:nil, nil];
[alertView show];
}
}
- (UIImage *)shrinkImage:(UIImage *)originalImage toSize:(CGSize)size {
/**
* 原理:w * h
1.如果原图为 100 * 50: original > target
而imageView为 50 * 50;
宽度减少,高度也会跟着减少,
2. 如果原图为 50 * 100 original < target
而imageView为50 * 50
高度减少,宽度也会跟着减少
3. 如果原图为 50 * 100 或者是 100 * 50 original == target
而imageView为50 *100 或者是 100 * 50
那么等比例缩放
*/
// 开启上下文
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
CGFloat originalScale = originalImage.size.width / originalImage.size.height; // 原始的比例
CGFloat targetScale = size.width / size.height;// 目标的比例
CGRect targetRect;// 目标frame
// 第一种情况
if (originalScale > targetScale) { // 如果原始比例 > 目标比例 即 新图在imageView不设置的话 不会为原比例
targetRect.size.width = size.width; // 宽度为imageview的宽度
targetRect.size.height = size.height * targetScale / originalScale; // 高度为 imageView高度 * 比例
targetRect.origin.x = 0.0f;
targetRect.origin.y = (size.height - targetRect.size.height) * 0.5; // 位于中‘心’
} else if (originalScale < targetScale) {
targetRect.size.height = size.height; // 高度为imageView的高度
targetRect.size.width = size.width * originalScale / targetScale;
targetRect.origin.x = (size.width - targetRect.size.width) * 0.5;
targetRect.origin.y = 0.0f;
} else {
targetRect = CGRectMake(0, 0, size.width, size.height);
}
// 渲染
[originalImage drawInRect:targetRect];
// 得到效果图
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
return finalImage;
}
#pragma mark - 获取相片
/** 拍照--from摄像头 */
- (IBAction)newPhotoPressed:(id)sender {
[self pickMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}
/** 相册读取 */
- (IBAction)takePicButtonPressed:(id)sender {
[self pickMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}
#pragma mark - UIImagePickerController 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// 获取类型
self.lastChooseMediaType = info[UIImagePickerControllerMediaType];
if ([self.lastChooseMediaType isEqual:(NSString *)kUTTypeImage]) {
UIImage *choosenImage = info[UIImagePickerControllerOriginalImage];
self.image = [self shrinkImage:choosenImage toSize:self.imageView.bounds.size];
} else if ([self.lastChooseMediaType isEqual:(NSString *)kUTTypeMovie]) {
self.movieUrl = info[UIImagePickerControllerMediaURL];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end