調用系統圖片庫以及攝像頭
判斷當前設備是否有攝像頭
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
創建UIAlertController選擇
- (void)openLocalPhoto
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"獲取圖片" message:@"請選擇方式" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *imagePikerController = [[UIImagePickerController alloc]init];
imagePikerController.delegate = self;
imagePikerController.allowsEditing = YES;
imagePikerController.sourceType = UIImagePickerControllerSourceTypeCamera;
MYLog(@"相機");
[self presentViewController:imagePikerController animated:YES completion:nil];
}];
UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"從相冊獲取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
MYLog(@"相冊");
[self presentViewController:imagePickerController animated:YES completion:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
MYLog(@"取消");
}];
[alertController addAction:defaultAction];
[alertController addAction:cancelAction];
[alertController addAction:defaultAction1];
[self presentViewController:alertController animated:YES completion:nil];
}```
###以下是使用相機技術需要遵守的兩個協議方法
_<UIImagePickerControllerDelegate,UINavigationControllerDelegate>_
點擊完成后怎么做
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *image = info[UIImagePickerControllerEditedImage];
self.headerImageView.image = image;
[self dismissViewControllerAnimated:picker completion:nil];
}```
點擊取消按鈕怎么做
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}```
###iOS 8.0以前使用的方法
遵守四個協議
_<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,UIAlertViewDelegate>_
-(void)open{
UIActionSheet *sht = [[UIActionSheet alloc]initWithTitle:@"請選擇"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"相機"
otherButtonTitles:@"相冊", nil];
}```
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 2) {
MYLog(@"取消");
}else if (buttonIndex == 1){
MYLog(@"相冊");
UIImagePickerController *imagePicker = [UIImagePickerController new];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}else{
MYLog(@"相機");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [UIImagePickerController new];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}else{
MYLog(@"無法識別到相機");
}
}
}```
##壓縮照片技術
-
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString ,id> )info
{
UIImage image = info[UIImagePickerControllerOriginalImage];
MYLog(@"image length = %ld",UIImagePNGRepresentation(image).length);//原圖
/ 縮略圖/
UIImage *newImage = [self thumnailWithImage:image size:CGSizeMake(100, 100)];
MYLog(@"newImage length = %ld",UIImagePNGRepresentation(newImage).length);/** 發送數據 二次壓縮圖*/
NSData *sendData = UIImageJPEGRepresentation(newImage, 0.05);//參數越高 質量越好0-1
MYLog(@"sendData length = %ld",sendData.length);
[self realSendImageMsg:sendData];[self dismissViewControllerAnimated:picker completion:nil];
}```
** 生成縮略圖**
-(UIImage *)thumnailWithImage:(UIImage *)image size:(CGSize)size{
UIImage *newImage = nil;
if (image != nil) {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newImage;
}```
** 發送圖片消息**
-(void)realSendImageMsg:(NSData *)data{
//把data 轉成文本 base64編碼
NSString *base64Str = [data base64EncodedStringWithOptions:0];
//構建圖片消息對象
XMPPMessage *msg = [XMPPMessage messageWithType:@"chat" to:self.friendJid];
[msg addBody:base64Str];
//發送消息
[[KRXmppTool sharedKRXmppTool].xmppStream sendElement:msg];
}```
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}