码迷,mamicode.com
首页 > 其他好文 > 详细

摄像头拍照上传

时间:2014-06-18 11:37:58      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:摄像头

       今天研究UIImagePickerControlle这个类,用它来打开摄像头和本地照片库。找到一个好的办法也是比较简单的方法来将选择好的图片显示我们想要的UIImageView上,下面通过代码来详细讲解.

这是我的ActionSheet 协议事件    

#pragma mark - ActionSheetDelegate

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

{

    photoPicker = [[PhotoPickerControlleralloc]initWithDelegate:self];

    photoPicker.useCameraName = actionSheet.title;

   if(buttonIndex == 0)

    {

        /*选择已有照片*/

        [selfLocalPhoto];

    }

   else if(buttonIndex ==1)

    {

        /*拍照从相机获取*/

        [selftakePhoto];

    }

}

图片:

bubuko.com,布布扣

当点击第一项时调用:

#pragma mark - 本地照片库

-(void)LocalPhoto

{

    UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

    picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;  //类型

    picker.delegate =self;  //协议

    picker.allowsEditing =YES;

    [selfpresentViewController:picker animated:YEScompletion:nil];

}

当点击第二项时调用:

#pragma mark - 从相机获取

-(void)takePhoto

{

    if ([UIImagePickerControllerisSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])

    {

        UIImagePickerController *picker = [[UIImagePickerControlleralloc] init];

        picker.delegate =self;

        picker.allowsEditing =YES;

        picker.sourceType =UIImagePickerControllerSourceTypeCamera;

        [selfpresentViewController:picker animated:YEScompletion:nil];

    }

   else

    {

        UIAlertView *alerView = [[UIAlertViewalloc]initWithTitle:@"无法拍照" message:@"此设备拍照功能不可用" delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];

        [alerViewshow];

    }

}


下面是你选择照片调用的协议方法:

#pragma mark - UIImagePickerCont≥rollerDelegate

/*当选择一张图片后进入*/

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

{

    NSString *type = [infoobjectForKey:UIImagePickerControllerMediaType];  //得到类型

    if([typeisEqualToString:(NSString *)kUTTypeImage])

    {

        UIImage *image = [infoobjectForKey:UIImagePickerControllerEditedImage];  //得到相机图片

       if (!image)

        {

            image = [info objectForKey:UIImagePickerControllerOriginalImage];

        }

        [selfperformSelector:@selector(uploadUserHeadPicture:)withObject:image afterDelay:0.01];

        [picker dismissViewControllerAnimated:YEScompletion:nil];

        

    }

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}


/*上传图片*/

- (void)uploadUserHeadPicture:(UIImage *)image

{

    NSArray *dirPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

   NSString * docsDir = [dirPaths objectAtIndex:0];  //得到documen目录下

   NSData *imageData = UIImagePNGRepresentation(image); //将图片转化为png

   databasePath = [NSStringstringWithFormat:@"%@/%@",docsDir,HeadImgName];//图片文件完整的路径

    [imageDatawriteToFile:databasePathatomically:YES];//将图片写入到文件中去


    UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"上传成功" delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];

    [alertshow];

    [self.mTableViewreloadData];

}

//此时主要代码已经完成如果你要显示你选择的图片只需通过

[UIImageimageWithContentsOfFile:databasePath];加载就是了。

HeadImgName 是个宏定义

#define HeadImgName @"head.png"


做完这些后可以通过iFunBox软件查看你的应用程序document目录下面是否保存有你刚刚上传的图片。

bubuko.com,布布扣


当然往往项目里面还是会把它保存到服务器中去。




摄像头拍照上传,布布扣,bubuko.com

摄像头拍照上传

标签:摄像头

原文地址:http://blog.csdn.net/itpeng523/article/details/31406731

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!