首先,照相或从相册选择照片需要使用UIImagePickerController,使用时需要添加两个协议
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *imageScroll;
@property (weak, nonatomic) IBOutlet UIImageView *photoImage;
@end
从相机获取图片的方式:
//从相机获取图片
- (IBAction)photoFromCamera:(id)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;//设置类型为相机
UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
picker.delegate = self;//设置代理
picker.allowsEditing = YES;//设置照片可编辑
picker.sourceType = sourceType;
//picker.showsCameraControls = NO;//默认为YES
//创建叠加层
UIView *overLayView=[[UIView alloc]initWithFrame:CGRectMake(0, 120, 320, 254)];
//取景器的背景图片,该图片中间挖掉了一块变成透明,用来显示摄像头获取的图片;
UIImage *overLayImag=[UIImage imageNamed:@"zhaoxiangdingwei.png"];
UIImageView *bgImageView=[[UIImageView alloc]initWithImage:overLayImag];
[overLayView addSubview:bgImageView];
picker.cameraOverlayView=overLayView;
picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;//选择前置摄像头或后置摄像头
[self presentViewController:picker animated:YES completion:^{
}];
}
else {
NSLog(@"该设备无相机");
}
}
从相册获取图片的方式:
//从相册获取图片
- (IBAction)photoFromAlbum:(id)sender {
UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
}
pickerImage.delegate = self;
pickerImage.allowsEditing = NO;
[self presentViewController:pickerImage animated:YES completion:^{
}];
}
从相册或相机选择图片之后的操作:
//从相册选择图片后操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:^{
}];
//NSLog(@"%@",info);
//保存原始图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self saveImage:image withName:@"currentImage.png"];
}
保存选择的图片并显示在控件上:
//保存图片
- (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];
//将选择的图片显示出来
[self.photoImage setImage:[UIImage imageWithContentsOfFile:fullPath]];
//将图片保存到disk
UIImageWriteToSavedPhotosAlbum(currentImage, nil, nil, nil);
}
取消操作时调用的方法:
//取消操作时调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
DEMO下载地址: http://download.csdn.net/download/u011918080/6932721