博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
照相功能
阅读量:6304 次
发布时间:2019-06-22

本文共 5589 字,大约阅读时间需要 18 分钟。

hot3.png

调用相机并且上传照片的方法

#import "ViewController.h"#import "AFNetworking.h"@interface ViewController () 
 @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];    /**     *  window代表着最高处,所有能看到的view,后面都是window。     *  当push相机控制器的时候,self.view就自动移除了。而当dismiss控制器的时候,因为self.view移除了,所有sheet无法寄居在view的上面,而固定在self.view.window,就可以保证,sheet一定在view视图上     */    [sheet showInView:self.view.window];     } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];         ipc.delegate = self;    ipc.allowsEditing = YES;  //相片是否可编辑     switch (buttonIndex) {        case 0:            if (![UIImagePickerController isSourceTypeAvailable:                  UIImagePickerControllerSourceTypeCamera]) return;                ipc.sourceType = UIImagePickerControllerSourceTypeCamera;            break;        case 1:            if (![UIImagePickerController isSourceTypeAvailable:                  UIImagePickerControllerSourceTypePhotoLibrary]) return;                ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;            break;                default:            return;            break;    }    [self presentViewController:ipc animated:YES completion:nil];} /** *  选择完照片后调用的方法 * * *  @param info   所有相片的信息都在这个字典 */-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{    [self dismissViewControllerAnimated:YES completion:nil];         //从字典key获取image的地址    UIImage *image = info[UIImagePickerControllerOriginalImage];         self.imageView.image = image;          } - (IBAction)upload:(UIButton *)sender {         AFHTTPRequestOperationManager *mrg = [[AFHTTPRequestOperationManager alloc] init];         NSMutableDictionary *params = [NSMutableDictionary dictionary];    params[@"username"] = @"123";           NSString *url = @"http://192.168.130.110:8080/MJServer/upload";         /**     *  上传时候调用的方法     *     *  params   post的参数,字典     *  formData formData是post上去的数据     *  name     服务器接受文件的名字     *  fileName 文件的名字     */    [mrg POST:url parameters:params constructingBodyWithBlock:^(id
 formData) {                NSData *data = UIImageJPEGRepresentation(self.imageView.image, 1.0);        [formData appendPartWithFileData:data name:@"file" fileName:@"1.jpg" mimeType:@"image/jpeg"];             } success:^(AFHTTPRequestOperation *operation, id responseObject) {                 NSLog(@"上传成功");             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {                NSLog(@"上传失败");    }]; }@end

2 手动存储相片到相册

    for (int i = 0; i<=9; i++) {        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"00%d.png", i]];        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);        NSLog(@"%@", image);        [NSThread sleepForTimeInterval:1];   //存储不可以过快    }

3 按钮文字设置成中文

info.plist 中添加Localized resources can be mixed 设置为YES

4 图片选择控制器

当想自己写一个图片选择控制器的时候,需要利用AssetsLibrary.framework,利用这个框架获得所有的相册图片

5 其他

switch (buttonIndex) {        case 0://照相机        { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];            imagePicker.delegate = self;            imagePicker.allowsEditing = YES;            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;            imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];            [self presentViewController:imagePicker animated:YES completion:nil];                    }            break;        case 1://摄像机        {            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];            imagePicker.delegate = self;            imagePicker.allowsEditing = YES;            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;            imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];            imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow;           [self presentViewController:imagePicker animated:YES completion:nil];                    }            break;        case 2://本地相簿        {            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];            imagePicker.delegate = self;            imagePicker.allowsEditing = YES;            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;            imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];           [self presentViewController:imagePicker animated:YES completion:nil];        }            break;        case 3://本地视频        {            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];            imagePicker.delegate = self;            imagePicker.allowsEditing = YES;            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;            imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];           [self presentViewController:imagePicker animated:YES completion:nil];                    }

转载于:https://my.oschina.net/u/2346786/blog/500726

你可能感兴趣的文章
linux下的权限问题
查看>>
教你如何使用Flutter和原生App混合开发
查看>>
Spring Boot 整合redis
查看>>
CSS hover改变背景图片过渡动画生硬
查看>>
JDBC(三)数据库连接和数据增删改查
查看>>
淘宝应对"双11"的技术架构分析
查看>>
ssh
查看>>
订单的子单表格设置颜色
查看>>
Office365 Exchange Hybrid 番外篇 ADFS后端SQL群集(一)
查看>>
9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路...
查看>>
lvs fullnat部署手册(三)rs内核加载toa篇
查看>>
C++策略模式
查看>>
我的友情链接
查看>>
oracle表分区详解
查看>>
网络编程中常见结构体
查看>>
SSL/TLS原理详解
查看>>
Docker 自定义SSH服务镜像
查看>>
JavaScript强化教程 —— Cocos2d-JS自动JSB绑定规则修改
查看>>
configure: error: in `/root/httpd-2.2.11/srclib/apr': c
查看>>
CentOS7搭建Kubernetes-dashboard管理服务
查看>>