```
先要遵循協議{//全局的UITableView * tableView;? ?
?UIButton * button;}- (void)viewDidLoad {??
? [super viewDidLoad];
//給button大小根據需要設置? ?
?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)];
//添加點擊方法? ? [button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];? ??
[self.view addSubview:button]; }
//button的點擊方法-(void)buttonAction{? ? ?
?UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@""preferredStyle:(UIAlertControllerStyleAlert)];??
? UIAlertAction * alert = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//調用相冊方法? ? ? ? [self fromPhotos];? ??
?}];? ?
?UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//調用相機的方法? ? ? ? [self fromCamera];??
? }];? ?
?UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ??
? //想要刪除已經添加上的方法? ? ?
?? [self UIButtonAction];? ?
?}];? ? ??
? [alertController addAction:deleteAction];? ? [alertController addAction:archiveAction];? ? [alertController addAction:cancelAction];? ? [alertController addAction:alert];
//推出視圖? ?
?[self presentViewController:alertController animated:YES completion:nil];? ??
}
//調用相冊方法
-(void)fromPhotos{? ?
?//創建UIImagePickerController對象??
? UIImagePickerController *imp=[[UIImagePickerController alloc]init];? ??
//設置UIImagePickerController類型(相冊/相機/圖片庫)? ? imp.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;? ??
//設置代理(目的:為了讓這個對象執行協議中的方法)??
? imp.delegate=self;? ??
//設置獲取出來的照片是否可以編輯? ??
imp.allowsEditing= YES;? ??
?//模態推出視圖??
? [self presentViewController:imp animated:YES completion:nil];? ? ? ? }
?-(void)fromCamera{??
? //先設定sourceType為相機,然后判斷相機是否可用(ipod)沒相機,不可用將sourceType設定為相片庫? ??
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;? ? if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {? ? ? ? UIImagePickerController * picker =[[UIImagePickerController alloc] init];? ? ? ? picker.delegate = self;? ? ?
?? //設置拍照后的圖片可被編輯? ? ? ? picker.allowsEditing = YES;? ? ? ? picker.sourceType = sourceType;
//? ? ? ? [self presentModalViewController:picker animated:YES];被廢棄的? ? ? ?
?[self presentViewController:picker animated:YES completion:nil];? ?
}
}
?//點擊圖庫中相片觸發事件,并將點擊德圖片返回//當選擇一張圖片后進入這里-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
NSString * type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:@"public.image"]) {
UIImage * originImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
//圖片壓縮,因為原圖都是很大的,不必要傳原圖
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);
}
//? ? ? ? //關閉相冊界面
//? ? ? ? [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;
}
//相機中文
在項目的info.plist里面添加 Localized resources can be mixed YES(表示是否允許應用程序獲取框架庫內語言)
```