我們在做應(yīng)用過程中,難免會遇到要設(shè)置用戶頭像這樣的功能,我這里總結(jié)了一個調(diào)用系統(tǒng)相機,相冊的功能實現(xiàn),寫出來與大家分享,如有不足還請指正: 1.我們在調(diào)用這個功能的時候,一般都有個用來填充圖片的ImageView和點擊ImageView觸發(fā)此方法的事件,這里我就寫個ImageView和Button來演示,下面是實現(xiàn)整個功能的代碼:
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 就是觸點的坐標(biāo)// 觸點在imageView內(nèi),點擊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){// 設(shè)置圖片放大動畫[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;? ? ? ? }? ? }// 跳轉(zhuǎn)到相機或相冊頁面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.奇怪的是我們不簽代理,功能也能實現(xiàn),為了保險起見,還是簽訂代理;到此,要實現(xiàn)的基本功能已經(jīng)完全展示出來了,我們再根據(jù)自己項目的需求來改動就可以了。 希望對大家有所幫助。