iOS调用系统相机,访问系统相册功能实现

96
张战威ican
2016.09.01 09:32* 字数 788

我们在做应用过程中,难免会遇到要设置用户头像这样的功能,我这里总结了一个调用系统相机,相册的功能实现,写出来与大家分享,如有不足还请指正: 1.我们在调用这个功能的时候,一般都有个用来填充图片的ImageView和点击ImageView触发此方法的事件,这里我就写个ImageView和Button来演示,下面是实现整个功能的代码:

import "ViewController.h"

define kWidth [UIScreen mainScreen].bounds.size.width

define kHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController () { BOOL isFullScreen; } @property(nonatomic,strong)UIImageView *imageView;

@end

@implementation ViewController

(void)viewDidLoad { [super viewDidLoad];

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

UIButton *chooseBtn = [UIButton buttonWithType:UIButtonTypeCustom]; chooseBtn.frame = CGRectMake(100, 30, 100, 40); [chooseBtn addTarget:self action:@selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside]; [chooseBtn setBackgroundColor:[UIColor brownColor]]; [chooseBtn setTitle:@"选择照片" forState:UIControlStateNormal]; [self.view addSubview:chooseBtn];

}

pragma mark - 保存图片至沙盒

(void) saveImage:(UIImage)currentImage withName:(NSString)imageName {

NSDataimageData = UIImageJPEGRepresentation(currentImage, 0.5); // 获取沙盒目录 NSStringfullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];

// 将图片写入文件 [imageData writeToFile:fullPath atomically:NO]; }

pragma mark - image picker delegte

(void)imagePickerController:(UIImagePickerController)picker didFinishPickingMediaWithInfo:(NSDictionary)info { [picker dismissViewControllerAnimated:YES completion:^{}];

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

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

NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];

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

isFullScreen = NO; [self.imageView setImage:savedImage];

self.imageView.tag = 100;

}

(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissViewControllerAnimated:YES completion:^{}]; }

-(void)touchesBegan:(NSSet)touches withEvent:(UIEvent)event {

isFullScreen = !isFullScreen;UITouch *touch = [touches anyObject];CGPointtouchPoint = [touch locationInView:self.view];CGPointimagePoint =self.imageView.frame.origin;//touchPoint.x ,touchPoint.y 就是触点的坐标// 触点在imageView内,点击imageView时 放大,再次点击时缩小if(imagePoint.x<= touchPoint.x&& imagePoint.x+self.imageView.frame.size.width>=touchPoint.x&& imagePoint.y<=  touchPoint.y&& imagePoint.y+self.imageView.frame.size.height>= touchPoint.y){// 设置图片放大动画[UIViewbeginAnimations:nilcontext:nil];// 动画时间[UIViewsetAnimationDuration:1];if(isFullScreen) {// 放大尺寸self.imageView.frame= CGRectMake(0,0, kWidth, kHeight);    }else{// 缩小尺寸self.imageView.frame= CGRectMake(100,100,200,200);    }// commit动画[UIViewcommitAnimations];}

}

pragma mark - actionsheet delegate

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 255) {

NSUInteger sourceType =0;// 判断是否支持相机if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {switch(buttonIndex) {case0:// 取消return;case1:// 相机sourceType = UIImagePickerControllerSourceTypeCamera;break;case2:// 相册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;    [selfpresentViewController:imagePickerController animated:YEScompletion:^{}];}

}

(void)chooseImage:(UIButton *)btn {

UIActionSheet *sheet;

// 判断是否支持相机 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil]; } else {

sheet = [[UIActionSheet alloc] initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"从相册选择",nil];

}

sheet.tag = 255;

[sheet showInView:self.view];

} @end

2.奇怪的是我们不签代理,功能也能实现,为了保险起见,还是签订代理;到此,要实现的基本功能已经完全展示出来了,我们再根据自己项目的需求来改动就可以了。 希望对大家有所帮助。

日记本