標籤:make with err animate bar key apple jpeg return
普通效果
1.添加協議
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
2.添加UIImagePickerController
@property (nonatomic, strong) UIImagePickerController *imagePick; //用於上傳圖片
3.初始化UIImagePickerController
self.imagePick = [[UIImagePickerController alloc]init]; self.imagePick.delegate = self;
4.彈框選擇
//選取相簿內的圖片- (void)changeHeadImageAction { UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; //按鈕:從相簿選擇,類型:UIAlertActionStyleDefault [alert addAction:[UIAlertAction actionWithTitle:@"從相簿選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { self.imagePick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // imagePick.mediaTypes = @[(NSString *)kUTTypeImage]; self.imagePick.allowsEditing = YES; [self presentViewController:self.imagePick animated:YES completion:nil]; }]]; //按鈕:拍照,類型:UIAlertActionStyleDefault [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) { self.imagePick.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:self.imagePick animated:YES completion:nil]; }else{ NSLog(@"網路攝影機貌似不支援..."); } }]]; //按鈕:取消,類型:UIAlertActionStyleCancel [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alert animated:YES completion:nil];}
5.實現協議方法
// 取消選擇照片- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil];}- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ [picker dismissViewControllerAnimated:NO completion:nil]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; //壓縮照片,這個base64就是需要上傳到伺服器的圖片資料 [image drawInRect:CGRectMake(0, 0, 150, 266)]; NSData *data = UIImageJPEGRepresentation(image, 0.0f); NSString *base64 = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; }-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ viewController.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor]; viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor]; //設定標題顏色 NSDictionary *dic=[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]; self.navigationController.navigationBar.titleTextAttributes = dic;}- (void)doCancel:(id)b { NSLog(@"doCancel"); [self.imagePick dismissViewControllerAnimated:YES completion:nil];}
6.設定相簿裡面的文字為漢字
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{// [NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:NSLangu; [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", nil] forKey:@"AppleLanguages"]; [[NSUserDefaults standardUserDefaults] synchronize]; return YES;}
然後在下面這個位置添加中文
ios 圖片上傳與壓縮,UIImagePickerController設定中文