我们在写代码的时候经常会将头像进行上传服务器,上传头像图片我试过两种方式

一种方式就是使用base64字符串上传图片,这种形式我个人认为比较适合上传图片数量比较少的,比如上传头像,上传图片数量多的话,速度会慢些
另一种方式是使用二进制流进行上传图片,这种方式上传图片少或者数量多都没关系,速度也很快
demo地址:http://download.csdn.net/detail/tuwanli125/9340205
demo地址: https://github.com/tuwanli/PictureHead
选择头像效果:
程序如下:
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutletUIImageView *headIcon; - (IBAction)changeIconAction:(UITapGestureRecognizer *)sender; @end
ViewController.m
#import "ViewController.h"
#import "AFHTTPRequestOperationManager.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
{
UIImagePickerController *pickerController;
AFHTTPRequestOperationManager *manager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//初始化头像控件
[selfinitHeadIcon];
//初始化pickController
[selfcreateData];
}
- (void)initHeadIcon
{
self.view.backgroundColor = [UIColorlightGrayColor];
self.headIcon.layer.cornerRadius = self.headIcon.frame.size.height/2;
self.headIcon.clipsToBounds =YES;
self.headIcon.layer.borderColor = [UIColor whiteColor].CGColor;
self.headIcon.layer.borderWidth = 3;
}
- (void)createData
{
//初始化pickerController
pickerController = [[UIImagePickerControlleralloc]init];
pickerController.view.backgroundColor = [UIColororangeColor];
pickerController.delegate =self;
pickerController.allowsEditing =YES;
}
- (IBAction)changeIconAction:(UITapGestureRecognizer *)sender {
UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:@"选择头像"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"相册",@"图库",nil];
[actionSheet showInView:[UIApplicationsharedApplication].keyWindow];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex ==0) {//相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@"支持相机");
[selfmakePhoto];
}else{
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"请在设置-->隐私-->相机,中开启本应用的相机访问权限!!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"我知道了",nil];
[alertshow];
}
}elseif (buttonIndex ==1){//相片
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
NSLog(@"支持相册");
[selfchoosePicture];
}else{
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"请在设置-->隐私-->照片,中开启本应用的相机访问权限!!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"我知道了",nil];
[alertshow];
}
}elseif (buttonIndex ==2){//图册
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
NSLog(@"支持图库");
[selfpictureLibrary];
// [self presentViewController:picker animated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"请在设置-->隐私-->照片,中开启本应用的相机访问权限!!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"我知道了",nil];
[alertshow];
}
}elseif (buttonIndex ==3){
}
}
//跳转到imagePicker里
- (void)makePhoto
{
pickerController.sourceType =UIImagePickerControllerSourceTypeCamera;
[selfpresentViewController:pickerControlleranimated:YEScompletion:nil];
}
//跳转到相册
- (void)choosePicture
{
pickerController.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[selfpresentViewController:pickerControlleranimated:YEScompletion:nil];
}
//跳转图库
- (void)pictureLibrary
{
pickerController.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentViewController:pickerControlleranimated:YEScompletion:nil];
}
//用户取消退出picker时候调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"%@",picker);
[pickerControllerdismissViewControllerAnimated:YEScompletion:^{
}];
}
//用户选中图片之后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"%s,info == %@",__func__,info);
UIImage *userImage = [selffixOrientation:[infoobjectForKey:@"UIImagePickerControllerOriginalImage"]];
userImage = [selfscaleImage:userImagetoScale:0.3];
//保存图片
// [self saveImage:userImage name:@"某个特定标示"];
[pickerControllerdismissViewControllerAnimated:YEScompletion:^{
}];
[self.headIconsetImage:userImage];
self.headIcon.contentMode = UIViewContentModeScaleAspectFill;
self.headIcon.clipsToBounds =YES;
//照片上传
[selfupDateHeadIcon:userImage];
}
- (void)upDateHeadIcon:(UIImage *)photo
{
//两种方式上传头像
/*方式一:使用NSData数据流传图片*/
NSString *imageURl =@"";
manager.responseSerializer = [AFHTTPResponseSerializerserializer];
manager.responseSerializer.acceptableContentTypes =[NSSetsetWithObject:@"text/html"];
[managerPOST:imageURlparameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(photo,1.0)name:@"text"fileName:@"test.jpg"mimeType:@"image/jpg"];
}success:^(AFHTTPRequestOperation *operation,id responseObject) {
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
}];
/*方式二:使用Base64字符串传图片*/
NSData *data =UIImageJPEGRepresentation(photo,1.0);
NSString *pictureDataString=[database64Encoding];
NSDictionary * dic =@{@"verbId":@"modifyUserInfo",@"deviceType":@"ios",@"userId":@"",@"photo":pictureDataString,@"mobileTel":@""};
[managerPOST:@""parameters:dic success:^(AFHTTPRequestOperation *operation,idresponseObject) {
if ([[responseObjectobjectForKey:@"flag"]intValue] == 0) {
}else{
}
}
failure:^(AFHTTPRequestOperation *operation,NSError *error) {
}];
}
//保存照片到沙盒路径(保存)
- (void)saveImage:(UIImage *)image name:(NSString *)iconName
{
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//写入文件
NSString *icomImage = iconName;
NSString *filePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.png", icomImage]];
// 保存文件的名称
// [[self getDataByImage:image] writeToFile:filePath atomically:YES];
[UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES];
}
//缩放图片
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
[imagedrawInRect:CGRectMake(0,0, image.size.width * scaleSize, image.size.height *scaleSize)];
UIImage *scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"%@",NSStringFromCGSize(scaledImage.size));
return scaledImage;
}
//修正照片方向(手机转90度方向拍照)
- (UIImage *)fixOrientation:(UIImage *)aImage {
// No-op if the orientation is already correct
if (aImage.imageOrientation ==UIImageOrientationUp)
return aImage;
CGAffineTransform transform =CGAffineTransformIdentity;
switch (aImage.imageOrientation) {
caseUIImageOrientationDown:
caseUIImageOrientationDownMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform =CGAffineTransformRotate(transform,M_PI);
break;
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width,0);
transform =CGAffineTransformRotate(transform,M_PI_2);
break;
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
transform =CGAffineTransformTranslate(transform,0, aImage.size.height);
transform =CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (aImage.imageOrientation) {
caseUIImageOrientationUpMirrored:
caseUIImageOrientationDownMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width,0);
transform =CGAffineTransformScale(transform, -1,1);
break;
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRightMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.height,0);
transform =CGAffineTransformScale(transform, -1,1);
break;
default:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx =CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage),0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;
default:
CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}
CGImageRef cgimg =CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImageimageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
此demo从相册选区图片使用的单选图片,如果想看多选图片显示在ScrollView中demo 地址:
https://github.com/tuwanli/PictureMutipleSelect
总结
以上所述是小编给大家介绍的iOS 图片上传使用base64或者二进制流上传头像功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# ios
# 图片上传
# base64
# iOS实现相册多选图片上传功能
# 详解IOS开发中图片上传时两种图片压缩方式的比较
# 移动端html5图片上传方法【更好的兼容安卓IOS和微信】
# iOS实现压缩图片上传功能
# 微信JSSDK多图片上传并且解决IOS系统上传一直加载的问题
# iOS实现录音转码MP3及转码BASE64上传示例
# 上传图片
# 上传
# 请在
# 我知道了
# 两种
# 访问权限
# 小编
# 跳转到
# 流进
# 数量多
# 在此
# 都没
# 给大家
# 想看
# 跳转
# 会将
# 试过
# 所述
# 回调
# 给我留言
相关文章:
相册网站制作软件,图片上的网址怎么复制?
微信小程序 五星评分(包括半颗星评分)实例代码
高性能网站服务器部署指南:稳定运行与安全配置优化方案
如何快速重置建站主机并恢复默认配置?
如何在企业微信快速生成手机电脑官网?
建站主机选哪种环境更利于SEO优化?
,有什么在线背英语单词效率比较高的网站?
如何在局域网内绑定自建网站域名?
湖南网站制作公司,湖南上善若水科技有限公司做什么的?
如何在Golang中引入测试模块_Golang测试包导入与使用实践
建站之星ASP如何实现CMS高效搭建与安全管理?
平台云上自助建站如何快速打造专业网站?
建站之星CMS建站配置指南:模板选择与SEO优化技巧
建站之星导航如何优化提升用户体验?
西安制作网站公司有哪些,西安货运司机用的最多的app或者网站是什么?
如何用花生壳三步快速搭建专属网站?
如何选择靠谱的建站公司加盟品牌?
赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?
如何在阿里云服务器自主搭建网站?
头像制作网站在线制作软件,dw网页背景图像怎么设置?
枣阳网站制作,阳新火车站打的到仙岛湖多少钱?
高配服务器限时抢购:企业级配置与回收服务一站式优惠方案
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
如何登录建站主机?访问步骤全解析
教学网站制作软件,学习*后期制作的网站有哪些?
jQuery 常见小例汇总
武汉网站如何制作,黄黄高铁武穴北站途经哪些村庄?
黑客如何利用漏洞与弱口令入侵网站服务器?
网站制作报价单模板图片,小松挖机官方网站报价?
如何确认建站备案号应放置的具体位置?
建站主机选择指南:服务器配置与SEO优化实战技巧
,网站推广常用方法?
建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析
制作充值网站的软件,做人力招聘为什么要自己交端口钱?
免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?
如何通过老薛主机一键快速建站?
如何在景安云服务器上绑定域名并配置虚拟主机?
建站VPS选购需注意哪些关键参数?
,南京靠谱的征婚网站?
大连网站设计制作招聘信息,大连投诉网站有哪些?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
小程序网站制作需要准备什么资料,如何制作小程序?
制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?
学校为何禁止电信移动建设网站?
建站之星如何助力企业快速打造五合一网站?
php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】
制作网站的软件下载免费,今日头条开宝箱老是需要下载怎么回事?
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
公司网站制作需要多少钱,找人做公司网站需要多少钱?
音乐网站服务器如何优化API响应速度?
*请认真填写需求信息,我们会在24小时内与您取得联系。