iOS 调用系统相机拍照相册功能

 写一个按钮直接复制下面代码 

视图必须继承  

<UIImagePickerControllerDelegate,UIPopoverControllerDelegate,UIActionSheetDelegate>代理

 

//取拍照片

- (void)showActionSheet

{

    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];

    

}

//上传头像

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

{

    if (actionSheet.tag == 255) {

        

        NSUInteger sourceType = 0;

        

        // 判断是否支持相机

        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:^{}];

        

      //  [imagePickerController release];

    }

}

#pragma mark - image picker delegte

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

{

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

    

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    /* 此处info 有六个值

     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)

     * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片

     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片

     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)

     * UIImagePickerControllerMediaURL;       // an NSURL

     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework

     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo

     */

    // 保存图片至本地,方法见下文

    [self saveImage:image withName:@"currentImage.png"];

       _fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];

    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:_fullPath];

    [_imageButton setBackgroundImage:savedImage forState:0];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

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

}

 

- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName

{    

    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);

    // 获取沙盒目录

        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];

    // 将图片写入文件    

    [imageData writeToFile:fullPath atomically:NO];

}

 

posted @ 2015-11-06 14:04  久久1234  阅读(1890)  评论(0编辑  收藏  举报