您的位置:首页 > 产品设计 > UI/UE

OC学习之道:UIImagePickerController的使用,使用UIImagePickerController调用本地摄像头,本地相册

2015-06-10 21:51 369 查看
//
//  RootViewController.m
//  InvokPhoto
//

//  Copyright (c) 2015年 朝夕. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()
@property (nonatomic, retain) UIImageView *imageView;
@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];

self.navigationItem.title = @"相册";

// 保存图片到相册
//   [self saveImage];

//访问相册
[self visitPhoto];

//调用摄像头
[self camera];

}

#pragma mark -- 访问相册
- (void)visitPhoto{
UIBarButtonItem *rigthBtn = [[UIBarButtonItem alloc] initWithTitle:@"访问相册" style:(UIBarButtonItemStyleDone) target:self action:@selector(selectPhoto:)];
self.navigationItem.rightBarButtonItem = rigthBtn;
[rigthBtn release];

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 350, 200, 200)];
_imageView.backgroundColor = [UIColor grayColor];
[self.view addSubview:_imageView];
[_imageView release];

}

//相册调用

- (void)selectPhoto:(UIBarButtonItem *)barBtn{

UIImagePickerController *imagePick = [[UIImagePickerController alloc] init];
[self presentViewController:imagePick animated:YES completion:^{
imagePick.delegate = self;
imagePick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

}];

[imagePick release];
}

#pragma mark --UIImagePickerController Delegate
// 相册图片选中后调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

NSLog(@"%@", info);// 打印出相应的key信息
UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
//    UIImagePickerControllerEditedImage  拍完照后可以进行编辑
self.imageView.image = image;
[picker dismissViewControllerAnimated:YES completion:^{

}];
}
// 取消按钮的点击事件
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

}

#pragma mark -- 保存图片到相册
- (void)saveImage{

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
imageView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imageView];
[imageView release];

NSString *urlString = @"http://www.huabian.com/uploadfile/2014/1118/20141118042305865.jpg";
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url]; // 主线程加载,一般不采用此方法

//把data转换成UIImage
UIImage *image = [UIImage imageWithData:data];
imageView.image = image;

//把image转成data
NSData *imageData = UIImageJPEGRepresentation(image, 1);
NSLog(@"%lu", (unsigned long)imageData.length);

// 将图片保存到相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

}

// 将图片保存在本地
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

NSLog(@"%@", error);
}

#pragma mark -- 调用摄像头
- (void)camera{
UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
button.tag = 200;
button.frame = CGRectMake(80, 100, 100, 20);
[button setTitle:@"摄像头" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(invokeCamera:) forControlEvents:(UIControlEventTouchUpInside)];

[self.view addSubview:button];

}

// 调用摄像头
- (void)invokeCamera:(UIButton *)button{
UIImagePickerController *imagePick = [[UIImagePickerController alloc] init];
[self presentViewController:imagePick animated:YES completion:^{

// 判断是否有后置摄像头
//        UIImagePickerControllerCameraDeviceFront ,为前置摄像头
BOOL iscamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (!iscamera) {
NSLog(@"没有摄像头");
return ;
}
imagePick.delegate = self;
imagePick.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePick.allowsEditing = YES; //拍完照可以进行编辑

}];

[imagePick release];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end

//
//  RootViewController.h
//  InvokPhoto
//

//  Copyright (c) 2015年 朝夕. All rights reserved.
//

#import

@interface RootViewController : UIViewController

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