记录于2013/7/4
加入框架:
MobileCoreServices.framework
MediaPlayer.framework
导入头文件:
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/UTCoreTypes.h>
代码:
if(![UIImagePickerControllerisSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
takePictureButton.hidden = YES; //无摄像头隐藏改按钮
}
imageFrame = imageView.frame;
实现:
- (IBAction)shootPictureOrVideo:(id)sender {
[selfgetMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}
- (IBAction)selectExistingPictureOrVideo:(id)sender {
[selfgetMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}
#pragma mark UIImagePickerController delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeImage]) {
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
self.image = shrunkenImage;
} elseif ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeMovie]) {
self.movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
}
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
#pragma mark -
//类扩张
staticUIImage *shrinkImage(UIImage *original, CGSize size) {
//设备物理屏幕像素倍数
CGFloat scale = [UIScreenmainScreen].scale;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale,
size.height * scale, , , colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context,
CGRectMake(, , size.width * scale, size.height * scale),
original.CGImage);
CGImageRef shrunken = CGBitmapContextCreateImage(context);
UIImage *final = [UIImageimageWithCGImage:shrunken]; CGContextRelease(context);
CGImageRelease(shrunken); return final;
}
- (void)updateDisplay {
if ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeImage]) {
imageView.image = image;
imageView.hidden = NO;
moviePlayerController.view.hidden = YES;
} elseif ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
[self.moviePlayerController.view removeFromSuperview];
self.moviePlayerController = [[MPMoviePlayerControlleralloc]
initWithContentURL:movieURL];
moviePlayerController.view.frame = imageFrame;
moviePlayerController.view.clipsToBounds = YES;
[self.viewaddSubview:moviePlayerController.view];
imageView.hidden = YES;
}
}
- (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType {
//判断设备是否具有该功能
NSArray *mediaTypes = [UIImagePickerController
availableMediaTypesForSourceType:sourceType];
if ([UIImagePickerControllerisSourceTypeAvailable:
sourceType] && [mediaTypes count] > ) {
NSArray *mediaTypes = [UIImagePickerController
availableMediaTypesForSourceType:sourceType];
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.mediaTypes = mediaTypes;
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
} else {
//设备不支持该功能,弹出提示
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing media"
message:@"Device doesn’t support that media source."
delegate:nil
cancelButtonTitle:@"Drat!"
otherButtonTitles:nil];
[alert show];
}
}