相機技術(shù)圖片壓縮技術(shù)

調(diào)用系統(tǒng)圖片庫以及攝像頭

判斷當前設(shè)備是否有攝像頭

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 

創(chuàng)建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];
    
}```

###以下是使用相機技術(shù)需要遵守的兩個協(xié)議方法

_<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以前使用的方法
遵守四個協(xié)議

_<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(@"無法識別到相機");
        }
    }
}```

##壓縮照片技術(shù)
  • (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);

    /** 發(fā)送數(shù)據(jù) 二次壓縮圖*/
    NSData *sendData = UIImageJPEGRepresentation(newImage, 0.05);//參數(shù)越高 質(zhì)量越好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;
}```
** 發(fā)送圖片消息**

-(void)realSendImageMsg:(NSData *)data{
//把data 轉(zhuǎn)成文本 base64編碼
NSString *base64Str = [data base64EncodedStringWithOptions:0];
//構(gòu)建圖片消息對象
XMPPMessage *msg = [XMPPMessage messageWithType:@"chat" to:self.friendJid];
[msg addBody:base64Str];
//發(fā)送消息
[[KRXmppTool sharedKRXmppTool].xmppStream sendElement:msg];
}```

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容