我们在做应用过程中,难免会遇到要设置用户头像这样的功能,我这里总结了一个调用系统相机,相册的功能实现,写出来与大家分享,如有不足还请指正:
1.我们在调用这个功能的时候,一般都有个用来填充图片的ImageView和点击ImageView触发此方法的事件,这里我就写个ImageView和Button来演示,下面是实现整个功能的代码:
#import ""
#define kWidth [UIScreen mainScreen].
#define kHeight [UIScreen mainScreen].
@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate>
{
BOOL isFullScreen;
}
@property(nonatomic,strong)UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
_imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100, 200, 200)];
_imageView.backgroundColor = [UIColorgrayColor];
[self.viewaddSubview:_imageView];
UIButton *chooseBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
chooseBtn.frame = CGRectMake(100, 30, 100, 40);
[chooseBtn addTarget:selfaction:@selector(chooseImage:)forControlEvents:UIControlEventTouchUpInside];
[chooseBtn setBackgroundColor:[UIColorbrownColor]];
[chooseBtn setTitle:@"选择照片"forState:UIControlStateNormal];
[self.viewaddSubview:chooseBtn];
}
#pragma mark - 保存图片至沙盒
- (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];
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YEScompletion:^{}];
UIImage *image = [infoobjectForKey:UIImagePickerControllerOriginalImage];
[selfsaveImage:imagewithName:@""];
NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@""];
UIImage *savedImage = [[UIImagealloc] initWithContentsOfFile:fullPath];
isFullScreen =NO;
[self.imageViewsetImage:savedImage];
self.imageView.tag =100;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[selfdismissViewControllerAnimated:YEScompletion:^{}];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
isFullScreen = !isFullScreen;
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
CGPoint imagePoint = self.imageView.frame.origin;
// , 就是触点的坐标
// 触点在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([UIImagePickerControllerisSourceTypeAvailable: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 = [[UIImagePickerControlleralloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = sourceType;
[self presentViewController:imagePickerControlleranimated:YEScompletion:^{}];
}
}
- (void)chooseImage:(UIButton *)btn {
UIActionSheet *sheet;
// 判断是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
sheet = [[UIActionSheetalloc] initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"拍照",@"从相册选择",nil];
}
else {
sheet = [[UIActionSheetalloc] initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"从相册选择",nil];
}
sheet.tag = 255;
[sheet showInView:self.view];
}
@end
2.奇怪的是我们不签代理,功能也能实现,为了保险起见,还是签订代理;到此,要实现的基本功能已经完全展示出来了,我们再根据自己项目的需求来改动就可以了。
3.希望对大家有所帮助。