欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 圖片上傳使用base64或者二進制流上傳頭像功能

 更新時間:2017年09月29日 11:04:16   投稿:mrr  
這篇文章主要介紹了iOS 圖片上傳使用base64或者二進制流上傳頭像功能,需要的朋友可以參考下

我們在寫代碼的時候經(jīng)常會將頭像進行上傳服務器,上傳頭像圖片我試過兩種方式

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

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

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數(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;
}
//修正照片方向(手機轉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從相冊選區(qū)圖片使用的單選圖片,如果想看多選圖片顯示在ScrollView中demo 地址:

https://github.com/tuwanli/PictureMutipleSelect

總結

以上所述是小編給大家介紹的iOS 圖片上傳使用base64或者二進制流上傳頭像功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • IOS開發(fā)代碼分享之設置UISearchBar的背景顏色

    IOS開發(fā)代碼分享之設置UISearchBar的背景顏色

    在項目開發(fā)中,我們經(jīng)常要用到UISearchBar,在網(wǎng)上看到了很多關于去除掉他背景色的方法,都已經(jīng)失效了,今天來分享一個正常使用的方法,希望能幫到大家
    2014-09-09
  • 基于IOS端微信分享失效的踩坑及解決方法

    基于IOS端微信分享失效的踩坑及解決方法

    下面小編就為大家分享一篇基于IOS端微信分享失效的踩坑及解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS支付寶使用方法詳解

    iOS支付寶使用方法詳解

    這篇文章主要為大家詳細介紹了iOS支付寶的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • ios12中遇到的帶input彈窗的錯位問題的解決方法

    ios12中遇到的帶input彈窗的錯位問題的解決方法

    這篇文章主要介紹了ios12中遇到的帶input彈窗的錯位問題的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • iOS 開發(fā)常用宏總結

    iOS 開發(fā)常用宏總結

    這篇文章主要介紹了iOS 開發(fā)常用宏總結的相關資料,需要的朋友可以參考下
    2016-09-09
  • 分享一些iOS開發(fā)實用的小技巧

    分享一些iOS開發(fā)實用的小技巧

    這篇文章主要給大家分享了一些iOS開發(fā)實用的小技巧,這些小技巧在大家開發(fā)iOS的時候還是相當實用,有需要的朋友們下面來一起看看吧。
    2016-09-09
  • iOS應用內實現(xiàn)跳轉到手機淘寶天貓的方法

    iOS應用內實現(xiàn)跳轉到手機淘寶天貓的方法

    這篇文章主要給大家介紹了關于iOS應用內如何實現(xiàn)跳轉到手機淘寶天貓的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。
    2017-12-12
  • iOS動畫解析之圓球加載動畫XLBallLoading的實現(xiàn)

    iOS動畫解析之圓球加載動畫XLBallLoading的實現(xiàn)

    加載動畫對大家來說都不陌生,我們在平時都會遇見,開發(fā)中也必不可少,所以下面這篇文章主要給大家介紹了關于iOS動畫解析之圓球加載動畫XLBallLoading實現(xiàn)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-11-11
  • iOS動態(tài)驗證碼實現(xiàn)代碼

    iOS動態(tài)驗證碼實現(xiàn)代碼

    本文通過實例代碼給大家介紹了ios動態(tài)驗證碼的實現(xiàn)方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2018-04-04
  • iOS中指紋識別常見問題匯總

    iOS中指紋識別常見問題匯總

    最近在公司做了一個app要使用指紋支付的功能,在實現(xiàn)過程中遇到各種坑,今天小編抽抗給大家總結把遇到問題匯總特此分享到腳本之家平臺,需要的朋友參考下
    2016-12-12

最新評論