相机技术图片压缩技术

调用系统图片库以及摄像头

判断当前设备是否有摄像头

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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,473评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,733评论 4 61
  • //我所经历的大数据平台发展史(三):互联网时代 • 上篇http://www.infoq.com/cn/arti...
    葡萄喃喃呓语阅读 51,566评论 10 200
  • 坐在城中村的一个四楼楼顶,仰头是一片灰黑。 “啪”,又打开一罐啤酒,就着漫天灰浊,仰头大饮。和远处数之不尽的大厦...
    西周阅读 1,625评论 0 0
  • 静静地,花开了; 风描述了叶的历程; 青苹果散发它诱人的芬芳; 零落在天空的晚霞; 淡化了默许的点点滴滴; 易朽的...
    初归a阅读 2,224评论 0 0

友情链接更多精彩内容