加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:42:49 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 1?调用相机并且上传照片的方法 #import?"ViewController.h"#import?"AFNetworking.h"@interface?ViewController?()?UIActionSheetDelegate,UIImagePic

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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

#import?"ViewController.h"
#import?"AFNetworking.h"
@interface?ViewController?()?<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
?
@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<AFMultipartFormData>?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);
????????NSLog(@"%@",?image);
????????[NSThread?sleepForTimeInterval:1];???//存储不可以过快
????}

3 按钮文字设置成中文


4 图片选择控制器

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读