IOS 相机,相册获取图片

UIImagePickerController拍照与摄像

该类继承自UINavigationController类

步骤:

检查媒体来源模式是否可用

检查该来源模式下所支持的媒体类型

创建图像选取控制器,设置其属性并显示

在委托协议方法中处理

1.检查媒体来源

调用UIImagePickerController类的静态方法isSourceTypeAvailable来检查

sourceType是一个UIImagePickerControllerSourceType类型的枚举值,它表示图像选取控制器的3种不同的媒体来源模式

UIImagePickerControllerSourceTypePhotoLibrary:照片库模式。图像选取控制器以该模式显示时会浏览系统照片库的根目录。

UIImagePickerControllerSourceTypeCamera:相机模式,图像选取控制器以该模式显示时可以进行拍照或摄像。

UIImagePickerControllerSourceTypeSavedPhotosAlbum:相机胶卷模式,图像选取控制器以该模式显示时会浏览相机胶卷目录。

如果设备支持指定的媒体来源模式,则isSourceTypeAvailable:方法返回YES,否则返回NO。

2.检查支持的媒体类型

调用UIImagePickerController类的另一个静态方法 availableMediaTypesForSourceType:

返回的是字符串数组,kUTTypeImage表示静态图片,kUTTypeMovie表示视频。这两个字符串常量定义在MobileCoreServices框架中。

- (IBAction)takePictureButtonClick:(id)sender{

//检查相机模式是否可用

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

NSLog(@"sorry, no camera or camera is unavailable.");

return;

}

//获得相机模式下支持的媒体类型

NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

BOOL canTakePicture = NO;

for (NSString* mediaType in availableMediaTypes) {

if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {

//支持拍照

canTakePicture = YES;

break;

}

}

//检查是否支持拍照

if (!canTakePicture) {

NSLog(@"sorry, taking picture is not supported.");

return;

}

//创建图像选取控制器

UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];

//设置图像选取控制器的来源模式为相机模式

imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

//设置图像选取控制器的类型为静态图像

imagePickerController.mediaTypes = [[[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil] autorelease];

//允许用户进行编辑

imagePickerController.allowsEditing = YES;

//设置委托对象

imagePickerController.delegate = self;

//以模视图控制器的形式显示

[self presentModalViewController:imagePickerController animated:YES];

[imagePickerController release];

}

- (IBAction)captureVideoButtonClick:(id)sender{

//检查相机模式是否可用

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

NSLog(@"sorry, no camera or camera is unavailable!!!");

return;

}

//获得相机模式下支持的媒体类型

NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

BOOL canTakeVideo = NO;

for (NSString* mediaType in availableMediaTypes) {

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

//支持摄像

canTakeVideo = YES;

break;

}

}

//检查是否支持摄像

if (!canTakeVideo) {

NSLog(@"sorry, capturing video is not supported.!!!");

return;

}

//创建图像选取控制器

UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];

//设置图像选取控制器的来源模式为相机模式

imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

//设置图像选取控制器的类型为动态图像

imagePickerController.mediaTypes = [[[NSArray alloc] initWithObjects:(NSString*)kUTTypeMovie, nil] autorelease];

//设置摄像图像品质

imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;

//设置最长摄像时间

imagePickerController.videoMaximumDuration = 30;

//允许用户进行编辑

imagePickerController.allowsEditing = YES;

//设置委托对象

imagePickerController.delegate = self;

//以模式视图控制器的形式显示

[self presentModalViewController:imagePickerController animated:YES];

[imagePickerController release];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

//打印出字典中的内容

NSLog(@"get the media info: %@", info);

//获取媒体类型

NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];

//判断是静态图像还是视频

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

//获取用户编辑之后的图像

UIImage* editedImage = [info objectForKey:UIImagePickerControllerEditedImage];

//将该图像保存到媒体库中

UIImageWriteToSavedPhotosAlbum(editedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])

{

//获取视频文件的url

NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];

//创建ALAssetsLibrary对象并将视频保存到媒体库

ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];

[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error) {

if (!error) {

NSLog(@"captured video saved with no error.");

}else

{

NSLog(@"error occured while saving the video:%@", error);

}

}];

[assetsLibrary release];

}

[picker dismissModalViewControllerAnimated:YES];

}

//load user image

- (void)UesrImageClicked

{

UIActionSheet *sheet;

// 判断是否支持相机

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

sheet= [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照", @"从相册选择", nil];

}

else {

sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];

}

sheet.tag = 255;

[sheet showInView:self.view];

}

#pragma mark - action sheet delegte

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

if (actionSheet.tag == 255) {

NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

// 判断是否支持相机

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

switch (buttonIndex) {

case 0:

return;

case 1: //相机

sourceType = UIImagePickerControllerSourceTypeCamera;

break;

case 2: //相册

sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

break;

}

}

else {

if (buttonIndex == 0) {

return;

} else {

sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

}

}

// 跳转到相机或相册页面

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

imagePickerController.delegate = self;

imagePickerController.allowsEditing = YES;

imagePickerController.sourceType = sourceType;

[self presentViewController:imagePickerController animated:YES completion:^{}];

}

}

#pragma mark - image picker delegte

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

[picker dismissViewControllerAnimated:YES completion:^{}];

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

userImageView.image = image;

NSData *imageData = UIImageJPEGRepresentation(image, COMPRESSED_RATE);

UIImage *compressedImage = [UIImage imageWithData:imageData];

[HttpRequestManager uploadImage:compressedImage httpClient:self.httpClient delegate:self];

}

创建一张表单,选择相机或者相册,然后用回调函数处理返回的图片。

但是返回的图像并不是正方形,显示在用户头像的View里面产生明显的拉伸。研究了很久各种裁剪算法,甚至想创建一个view来处理。突然发现代码中从相机、相册获取图片时采用的的

UIImagePickerControllerOriginalImage

于是跟踪进去一看:

UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType;      // an NSString (UTI, i.e. kUTTypeImage)

UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage;  // a UIImage

UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage;    // a UIImage

UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect;       // an NSValue (CGRect)

UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL;       // an NSURL

UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL        NS_AVAILABLE_IOS(4_1);  // an NSURL that references an

恍然大悟,很简单,用UIImagePickerControllerEditedImage,万事搞定。

你可能感兴趣的:(IOS 相机,相册获取图片)