最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ios利用 AFN 上傳相冊(cè)或者拍照?qǐng)D片

 更新時(shí)間:2017年06月22日 15:24:15   作者:smile麗語  
這篇文章主要介紹了ios利用 AFN 上傳相冊(cè)或者拍照?qǐng)D片的相關(guān)資料,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

由于項(xiàng)目中多處需要上傳圖片,我們可以自定義上傳圖片請(qǐng)求,自定義調(diào)取相冊(cè)及拍照,方便多處使用時(shí)調(diào)用。

主要步驟:

1.第一步:請(qǐng)求上傳你選取的相冊(cè)圖片或者拍照?qǐng)D片(經(jīng)過壓縮處理)

2.第二步:獲取到第一步圖片url上傳給服務(wù)器

3.第三步:回顯圖片(當(dāng)然進(jìn)入該界面時(shí)先判斷是否有圖片,無圖則展示占位圖片,否則就回顯圖片)

廢話不多說,直接上代碼:

1)封裝的上傳圖片的網(wǎng)絡(luò)請(qǐng)求(圖片壓縮) QTXUploadImage 文件

// 利用 afn 上傳一張圖片
#import <Foundation/Foundation.h>

@interface QTXUploadImage : NSObject

// 上傳圖片的網(wǎng)絡(luò)請(qǐng)求(圖片壓縮)
+ (void)post:(NSString *)url image:(UIImage *)image name:(NSString *)name success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;

@end

#import "QTXUploadImage.h"
#import "AFNetworking.h"
#import "QTXAccount.h"
#import "QTXAccountTool.h"

@implementation QTXUploadImage

/**
 * 上傳圖片的網(wǎng)絡(luò)請(qǐng)求(圖片壓縮)
 *
 * @param url       上傳圖片的網(wǎng)絡(luò)請(qǐng)求地址
 * @param name       和后臺(tái)包名一致
 *
 */
+ (void)post:(NSString *)url image:(UIImage *)image name:(NSString *)name success:(void (^)(id json))success failure:(void (^)(NSError *error))failure {

  // 1.創(chuàng)建網(wǎng)絡(luò)管理者
  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

  // 2.拼接請(qǐng)求參數(shù)url 也可在具體控制器請(qǐng)求里傳入
  NSDictionary *dict = @{@"userId" : [QTXAccountTool account].userId};

  // 3.發(fā)送請(qǐng)求
  [manager POST:url parameters:dict constructingBodyWithBlock:
   ^void(id<AFMultipartFormData> formData) {

     NSData *imageData = UIImageJPEGRepresentation(image, 0.5);//進(jìn)行圖片壓縮

     // 使用日期生成圖片名稱
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     formatter.dateFormat = @"yyyyMMddHHmmss";
     NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
     // 任意的二進(jìn)制數(shù)據(jù)MIMEType application/octet-stream
     [formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:@"image/png"];

   } success:^void(NSURLSessionDataTask * task, id responseObject) {

     if (success) {
       success(responseObject);
     }

   } failure:^void(NSURLSessionDataTask * task, NSError * error) {

     if (failure) {
       failure(error);
     }
   }];
}
@end

2)封裝的拍照/從相冊(cè)選擇 QTXImagePicker 文件

// 拍照/從相冊(cè)選擇
#import <Foundation/Foundation.h>

typedef void(^QTXImagePickerFinishAction)(UIImage *image);

@interface QTXImagePicker : NSObject

/**
 @param viewController 用于present UIImagePickerController對(duì)象
 @param allowsEditing  是否允許用戶編輯圖像
 */
+ (void)showImagePickerFromViewController:(UIViewController *)viewController
              allowsEditing:(BOOL)allowsEditing
               finishAction:(QTXImagePickerFinishAction)finishAction;


@end
#import "QTXImagePicker.h"

@interface QTXImagePicker()<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, weak) UIViewController *viewController;
@property (nonatomic, copy) QTXImagePickerFinishAction finishAction;
@property (nonatomic, assign) BOOL allowsEditing;

@end

static QTXImagePicker *qtxImagePickerInstance = nil;

@implementation QTXImagePicker

+ (void)showImagePickerFromViewController:(UIViewController *)viewController allowsEditing:(BOOL)allowsEditing finishAction:(QTXImagePickerFinishAction)finishAction {
  if (qtxImagePickerInstance == nil) {
    qtxImagePickerInstance = [[QTXImagePicker alloc] init];
  }

  [qtxImagePickerInstance showImagePickerFromViewController:viewController
                        allowsEditing:allowsEditing
                        finishAction:finishAction];
}

- (void)showImagePickerFromViewController:(UIViewController *)viewController
              allowsEditing:(BOOL)allowsEditing
               finishAction:(QTXImagePickerFinishAction)finishAction {
  _viewController = viewController;
  _finishAction = finishAction;
  _allowsEditing = allowsEditing;

  UIActionSheet *sheet = nil;

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    sheet = [[UIActionSheet alloc] initWithTitle:nil
                      delegate:self
                  cancelButtonTitle:@"取消"
               destructiveButtonTitle:nil
                  otherButtonTitles:@"拍照", @"從相冊(cè)選擇", nil];
  }else {
    sheet = [[UIActionSheet alloc] initWithTitle:nil
                      delegate:self
                  cancelButtonTitle:@"取消"
               destructiveButtonTitle:nil
                  otherButtonTitles:@"從相冊(cè)選擇", nil];
  }

  UIView *window = [UIApplication sharedApplication].keyWindow;
  [sheet showInView:window];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
  if ([title isEqualToString:@"拍照"]) {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.allowsEditing = _allowsEditing;
    [_viewController presentViewController:picker animated:YES completion:nil];

  }else if ([title isEqualToString:@"從相冊(cè)選擇"]) {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//    [picker.navigationBar setBarTintColor:QTXNavColor]; // 修改相冊(cè)的導(dǎo)航條的背景顏色
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [_viewController presentViewController:picker animated:YES completion:nil];

  }else {
    qtxImagePickerInstance = nil;
  }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  UIImage *image = info[UIImagePickerControllerEditedImage];
  if (image == nil) {
    image = info[UIImagePickerControllerOriginalImage];
  }

  if (_finishAction) {
    _finishAction(image);
  }

  [picker dismissViewControllerAnimated:YES completion:^{}];

  qtxImagePickerInstance = nil;
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  if (_finishAction) {
    _finishAction(nil);
  }

  [picker dismissViewControllerAnimated:YES completion:^{}];

  qtxImagePickerInstance = nil;
}

@end

當(dāng)使用時(shí),在所需使用的控制器里調(diào)用:

[QTXImagePicker showImagePickerFromViewController:self allowsEditing:YES finishAction:^(UIImage *image) {
      if (image) {

        self.idSideImage = image;
        [QTXUploadImage post:QTX_xsz1Url image:image name:@"xsz1" success:^(id json) {
          // 第一步請(qǐng)求上傳
          QTXLog(@"圖像上傳請(qǐng)求成功 %@", json);

          self.idSide = json[@"data"];

        } failure:^(NSError *error) {
          QTXLog(@"學(xué)生證圖像上傳請(qǐng)求失敗 %@", error);
        }];
      }
    }];

第二步和第三步 需要和各位自己后臺(tái)server聯(lián)調(diào),就屬于普通的get/post請(qǐng)求了,這邊就不放代碼了喲

插一下,我們產(chǎn)品沒讓拍照的照片存儲(chǔ)到相冊(cè)

// 存儲(chǔ)圖片名稱:001.png ~ 009.png

for (int i = 1; i<=9; i++) {
   UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"00%d.png", i]];

   UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

   [NSThread sleepForTimeInterval:1];
  }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS仿微信圖片分享界面實(shí)現(xiàn)代碼

    iOS仿微信圖片分享界面實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了iOS仿微信相冊(cè)界面翻轉(zhuǎn)過渡動(dòng)畫效果,微信采用界面翻轉(zhuǎn)的過渡動(dòng)畫跳轉(zhuǎn)到評(píng)論界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 詳解Objective-C設(shè)計(jì)模式編程中對(duì)備忘錄模式的運(yùn)用

    詳解Objective-C設(shè)計(jì)模式編程中對(duì)備忘錄模式的運(yùn)用

    這篇文章主要介紹了Objective-C設(shè)計(jì)模式編程中對(duì)備忘錄模式的運(yùn)用,文中結(jié)合了Cocoa框架下應(yīng)用的實(shí)例來加以講解,需要的朋友可以參考下
    2016-03-03
  • iOS仿抖音視頻加載動(dòng)畫效果的實(shí)現(xiàn)方法

    iOS仿抖音視頻加載動(dòng)畫效果的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于iOS視頻加載動(dòng)畫效果的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • iOS對(duì)數(shù)組進(jìn)行排序的實(shí)例代碼

    iOS對(duì)數(shù)組進(jìn)行排序的實(shí)例代碼

    本文通過實(shí)例代碼給大家講解了ios對(duì)數(shù)組進(jìn)行排序的實(shí)例方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧
    2017-08-08
  • 最新評(píng)論

    来安县| 张家口市| 泽库县| 故城县| 榆树市| 弋阳县| 开平市| 沅陵县| 祁东县| 丹凤县| 丹棱县| 象州县| 新泰市| 吴堡县| 湘潭县| 东莞市| 岑溪市| 大石桥市| 连山| 金川县| 平和县| 喜德县| 章丘市| 福泉市| 安庆市| 大足县| 繁峙县| 永定县| 富顺县| 高台县| 武夷山市| 兴仁县| 肥乡县| 万载县| 广元市| 成安县| 建昌县| 北票市| 齐齐哈尔市| 平远县| 汽车|