```
先要遵循協(xié)議{//全局的UITableView * tableView;? ?
?UIButton * button;}- (void)viewDidLoad {??
? [super viewDidLoad];
//給button大小根據(jù)需要設(shè)置? ?
?button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];?
?? button.frame =CGRectMake(10, 10, 50, 50);
? ? button.layer.cornerRadius = button.frame.size.width /2;? ? button.clipsToBounds = YES;? ??
? ? [self UIButtonAction];??
?}
//把給button賦圖片的代碼抽成方法
-(void)UIButtonAction{? ?
?[button setBackgroundImage:[UIImage imageNamed:@"add.png"] forState:(UIControlStateNormal)];
//添加點(diǎn)擊方法? ? [button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];? ??
[self.view addSubview:button]; }
//button的點(diǎn)擊方法-(void)buttonAction{? ? ?
?UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@""preferredStyle:(UIAlertControllerStyleAlert)];??
? UIAlertAction * alert = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"相冊(cè)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//調(diào)用相冊(cè)方法? ? ? ? [self fromPhotos];? ??
?}];? ?
?UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//調(diào)用相機(jī)的方法? ? ? ? [self fromCamera];??
? }];? ?
?UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ??
? //想要?jiǎng)h除已經(jīng)添加上的方法? ? ?
?? [self UIButtonAction];? ?
?}];? ? ??
? [alertController addAction:deleteAction];? ? [alertController addAction:archiveAction];? ? [alertController addAction:cancelAction];? ? [alertController addAction:alert];
//推出視圖? ?
?[self presentViewController:alertController animated:YES completion:nil];? ??
}
//調(diào)用相冊(cè)方法
-(void)fromPhotos{? ?
?//創(chuàng)建UIImagePickerController對(duì)象??
? UIImagePickerController *imp=[[UIImagePickerController alloc]init];? ??
//設(shè)置UIImagePickerController類型(相冊(cè)/相機(jī)/圖片庫(kù))? ? imp.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;? ??
//設(shè)置代理(目的:為了讓這個(gè)對(duì)象執(zhí)行協(xié)議中的方法)??
? imp.delegate=self;? ??
//設(shè)置獲取出來(lái)的照片是否可以編輯? ??
imp.allowsEditing= YES;? ??
?//模態(tài)推出視圖??
? [self presentViewController:imp animated:YES completion:nil];? ? ? ? }
?-(void)fromCamera{??
? //先設(shè)定sourceType為相機(jī),然后判斷相機(jī)是否可用(ipod)沒相機(jī),不可用將sourceType設(shè)定為相片庫(kù)? ??
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;? ? if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {? ? ? ? UIImagePickerController * picker =[[UIImagePickerController alloc] init];? ? ? ? picker.delegate = self;? ? ?
?? //設(shè)置拍照后的圖片可被編輯? ? ? ? picker.allowsEditing = YES;? ? ? ? picker.sourceType = sourceType;
//? ? ? ? [self presentModalViewController:picker animated:YES];被廢棄的? ? ? ?
?[self presentViewController:picker animated:YES completion:nil];? ?
}
}
?//點(diǎn)擊圖庫(kù)中相片觸發(fā)事件,并將點(diǎn)擊德圖片返回//當(dāng)選擇一張圖片后進(jìn)入這里-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
NSString * type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:@"public.image"]) {
UIImage * originImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
//圖片壓縮,因?yàn)樵瓐D都是很大的,不必要傳原圖
UIImage * image = [self scaleImagr:originImage toScale:0.3];
NSData * data =[[ NSData alloc] init];
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1.0);
}else{
data = UIImagePNGRepresentation(image);
}
//? ? ? ? //關(guān)閉相冊(cè)界面
//? ? ? ? [picker dismissModalViewControllerAnimated:YES];棄用的
[picker dismissViewControllerAnimated:YES completion:nil];
UIImageView * imageV =[[UIImageView alloc] init];
imageV.layer.masksToBounds= YES;
imageV.layer.cornerRadius = 10;
imageV.image = image;
[button setBackgroundImage:image forState:(UIControlStateNormal)];
}
}
#pragma mark- 縮放圖片
-(UIImage *)scaleImagr:(UIImage *)image toScale:(float)scaleSize{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPopContext();
return scledImage;
}
//相機(jī)中文
在項(xiàng)目的info.plist里面添加 Localized resources can be mixed YES(表示是否允許應(yīng)用程序獲取框架庫(kù)內(nèi)語(yǔ)言)
```