国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS 圖片上傳使用base64或者二進(jìn)制流上傳頭像功能

2019-10-21 18:44:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

我們?cè)趯懘a的時(shí)候經(jīng)常會(huì)將頭像進(jìn)行上傳服務(wù)器,上傳頭像圖片我試過(guò)兩種方式

一種方式就是使用base64字符串上傳圖片,這種形式我個(gè)人認(rèn)為比較適合上傳圖片數(shù)量比較少的,比如上傳頭像,上傳圖片數(shù)量多的話,速度會(huì)慢些

另一種方式是使用二進(jìn)制流進(jìn)行上傳圖片,這種方式上傳圖片少或者數(shù)量多都沒(méi)關(guān)系,速度也很快

demo地址:http://download.csdn.net/detail/tuwanli125/9340205

demo地址:  https://github.com/tuwanli/PictureHead

選擇頭像效果:

ios,圖片上傳,base64

程序如下:

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:@"拍照",@"相冊(cè)",@"圖庫(kù)",nil]; [actionSheet showInView:[UIApplicationsharedApplication].keyWindow];}- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex ==0) {//相機(jī)  if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {   NSLog(@"支持相機(jī)");   [selfmakePhoto];  }else{   UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"請(qǐng)?jiān)谠O(shè)置-->隱私-->相機(jī),中開(kāi)啟本應(yīng)用的相機(jī)訪問(wèn)權(quán)限!!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"我知道了",nil];   [alertshow];  } }elseif (buttonIndex ==1){//相片  if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])  {   NSLog(@"支持相冊(cè)");   [selfchoosePicture];  }else{   UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"請(qǐng)?jiān)谠O(shè)置-->隱私-->照片,中開(kāi)啟本應(yīng)用的相機(jī)訪問(wèn)權(quán)限!!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"我知道了",nil];   [alertshow];  } }elseif (buttonIndex ==2){//圖冊(cè)  if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])  {   NSLog(@"支持圖庫(kù)");   [selfpictureLibrary];//   [self presentViewController:picker animated:YES completion:nil];  }else{   UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"請(qǐng)?jiān)谠O(shè)置-->隱私-->照片,中開(kāi)啟本應(yīng)用的相機(jī)訪問(wèn)權(quán)限!!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"我知道了",nil];   [alertshow];  } }elseif (buttonIndex ==3){ }}//跳轉(zhuǎn)到imagePicker里- (void)makePhoto{ pickerController.sourceType =UIImagePickerControllerSourceTypeCamera; [selfpresentViewController:pickerControlleranimated:YEScompletion:nil];}//跳轉(zhuǎn)到相冊(cè)- (void)choosePicture{ pickerController.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum; [selfpresentViewController:pickerControlleranimated:YEScompletion:nil];}//跳轉(zhuǎn)圖庫(kù)- (void)pictureLibrary{ pickerController.sourceType =UIImagePickerControllerSourceTypePhotoLibrary; [selfpresentViewController:pickerControlleranimated:YEScompletion:nil];}//用戶取消退出picker時(shí)候調(diào)用- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ NSLog(@"%@",picker); [pickerControllerdismissViewControllerAnimated:YEScompletion:^{ }];}//用戶選中圖片之后的回調(diào)- (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:@"某個(gè)特定標(biāo)示"]; [pickerControllerdismissViewControllerAnimated:YEScompletion:^{ }]; [self.headIconsetImage:userImage]; self.headIcon.contentMode = UIViewContentModeScaleAspectFill; self.headIcon.clipsToBounds =YES; //照片上傳 [selfupDateHeadIcon:userImage];}- (void)upDateHeadIcon:(UIImage *)photo{ //兩種方式上傳頭像 /*方式一:使用NSData數(shù)據(jù)流傳圖片*/ 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;}//修正照片方向(手機(jī)轉(zhuǎn)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從相冊(cè)選區(qū)圖片使用的單選圖片,如果想看多選圖片顯示在ScrollView中demo 地址:

https://github.com/tuwanli/PictureMutipleSelect

總結(jié)

以上所述是小編給大家介紹的iOS 圖片上傳使用base64或者二進(jìn)制流上傳頭像功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 锡林郭勒盟| 浦北县| 固始县| 和林格尔县| 佳木斯市| 肃宁县| 武义县| 乌什县| 遂平县| 民权县| 英超| 蒙城县| 东至县| 大洼县| 台山市| 彰化市| 通道| 云阳县| 冀州市| 龙川县| 长海县| 弥勒县| 双鸭山市| 东乡族自治县| 泰来县| 分宜县| 泌阳县| 洪洞县| 冷水江市| 繁峙县| 尚志市| 拜泉县| 花莲县| 措勤县| 彰武县| 嘉荫县| 佛坪县| 安徽省| 祁东县| 崇礼县| 宜黄县|