您的位置:首页 > 移动开发 > IOS开发

ios--笔记--调用相机拍照及获取图片

2014-05-07 08:41 381 查看
#import "UIImage+OpenCV.h"

#import "MyViewController.h"
#import "OMGToast.h"
#import <QuartzCore/QuartzCore.h>

@interface MyViewController ()
- (void)processFrame;
@end

@implementation MyViewController

@synthesize imageViewSrc = _imageViewSrc;
@synthesize imageViewDesc = _imageViewDesc;
@synthesize imageViewTemp = _imageViewTemp;
@synthesize imgTmp = _imgTmp;
@synthesize imgSrc = _imgSrc;
@synthesize imgDesc = _imgDesc;

- (void)viewDidLoad
{
[super viewDidLoad];

}

-(IBAction)selectResPic:(id)sender
{

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = (id)self;
picker.allowsEditing = YES;

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//混合类型 photo + movie
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
}
[self presentModalViewController:picker animated:YES];
}

-(IBAction)selectDescPic:(id)sender
{

UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照", @"从手机相册选择",nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
//拍照
UIImagePickerController *camera = [[UIImagePickerController alloc] init];
camera.delegate = self;
camera.allowsEditing = YES;
//isCamera = TRUE;

//检查摄像头是否支持摄像机模式
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
camera.sourceType = UIImagePickerControllerSourceTypeCamera;
camera.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
NSLog(@"Camera not exist");
return;
}

[self presentViewController:camera animated:YES completion:nil];
}else if (buttonIndex == 1) {
//手机相册选择
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//混合类型 photo + movie
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
}
[self presentModalViewController:picker animated:YES];
}else if(buttonIndex == 2) {
//取消

}
}
- (void)actionSheetCancel:(UIActionSheet *)actionSheet{

}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{

}
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{

}

- (void)viewDidUnload
{
[super viewDidUnload];
self.imageViewSrc = nil;
self.imageViewDesc=nil;
delete _videoCapture;
_videoCapture = nil;
}

#pragma mark -
#pragma mark ImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:NO];

NSLog(@"info = %@",info);

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if([mediaType isEqualToString:@"public.image"])	//被选中的是图片
{
//获取照片实例
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

NSString *fileName = [[NSString alloc] init];

if ([info objectForKey:UIImagePickerControllerReferenceURL]) {
fileName = [[info objectForKey:UIImagePickerControllerReferenceURL] absoluteString];
//ReferenceURL的类型为NSURL 无法直接使用  必须用absoluteString 转换,照相机返回的没有UIImagePickerControllerReferenceURL,会报错
fileName = [self getFileName:fileName];
}
else
{
fileName = [self timeStampAsString];
}

NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];

[myDefault setValue:fileName forKey:@"fileName"];

//to-do

}

-(NSString *)getFileName:(NSString *)fileName
{
NSArray *temp = [fileName componentsSeparatedByString:@"&ext="];
NSString *suffix = [temp lastObject];

temp = [[temp objectAtIndex:0] componentsSeparatedByString:@"?id="];

NSString *name = [temp lastObject];

name = [name stringByAppendingFormat:@".%@",suffix];
return name;
}
-(NSString *)timeStampAsString
{
NSDate *nowDate = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEE-MMM-d"];
NSString *locationString = [df stringFromDate:nowDate];
return [locationString stringByAppendingFormat:@".png"];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: