首页 技术 正文
技术 2022年11月8日
0 收藏 473 点赞 1,934 浏览 3434 个字

IOS上传文件开发

    在移动应用开发  文件形式上传是不可缺少的,近期把IOS这块文件上传文件代码简单的整理一下。假设大家有须要安卓这边的代码,本人也能够分享给大家!

QQ群:74432915  欢迎大家一起探讨

首先本demo採用网上开源框架 AFNetworking  源代码:http://download.csdn.net/detail/wangliang198901/7809439
将整个框架导入IOS新建立的project中
在FKAppDelegate.h声明 例如以下:     

#import <UIKit/UIKit.h>

#import "AFHTTPRequestOperationManager.h"

@interface FKAppDelegate :UIResponder <UIApplicationDelegate>

@property (strong,nonatomic)UIWindow *window;

@property (strong,nonatomic)AFHTTPRequestOperationManager*
manager;

@end

然后在 FKAppDelegate.m文件初始化

#import "FKAppDelegate.h"

@implementation FKAppDelegate

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions

{

self.manager = [AFHTTPRequestOperationManagermanager];

self.manager.responseSerializer = [[AFHTTPResponseSerializeralloc]init];

return
YES;

}

然后在自定义ViewController主要做例如以下操作

#import "FKViewController.h"

#import "FKAppDelegate.h"

@interface
FKViewController ()

{

FKAppDelegate* appDelegate;

NSArray* images;

}

@end

@implementation FKViewController

– (void)viewDidLoad

{

[superviewDidLoad];

appDelegate = [UIApplicationsharedApplication].delegate;

self.picker.dataSource
= self;

self.picker.delegate
= self;

//
使用简化语法创建NSArray集合

images =@[@"logo",@"java"
, @"android"];

}

// UIPickerViewDataSource中定义的方法。该方法返回值决定该控件包括多少列

– (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

{

//返回1表明该控件仅仅包括1列

;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView

numberOfRowsInComponent:(NSInteger)component

{

returnimages.count;

}

#define kImageTag

– (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:

(NSInteger)row forComponent:(NSInteger)component

reusingView:(UIView *)view

{

//假设可重用的view的tag不等于kImageTag,表明该view已经不存在,须要又一次创建

if(view.tag !=kImageTag)

{

view = [[UIViewalloc]init];

//
为该UIView设置tag属性

view.tag =kImageTag;

//设置不同意用户交互

view.userInteractionEnabled =NO;

UIImageView* iv = [[UIImageViewalloc]initWithImage:

[UIImageimageNamed:[imagesobjectAtIndex:row]]];

iv. , 
, ,);

iv.contentMode =UIViewContentModeScaleAspectFit;

[viewaddSubview:iv];

}

return view;

}

// UIPickerViewDelegate中定义的方法,该方法的返回值决定列表项的高度

– (CGFloat)pickerView:(UIPickerView *)pickerView

rowHeightForComponent:(NSInteger)component

{

;

}

// UIPickerViewDelegate中定义的方法,该方法的返回值决定列表项的宽度

– (CGFloat)pickerView:(UIPickerView *)pickerView

widthForComponent:(NSInteger)component

{

;

}

– (IBAction)upload:(id)sender

{

//获取用户选中的行

];

//获取用户选中的文件名称

NSString* fileName = [imagesobjectAtIndex:selectedRow];

//依据用户选中的文件名称确定须要上传的文件

NSURL *filePath = [[NSBundlemainBundle]URLForResource:fileName

withExtension:@"png"];

NSDictionary *parameters =@{@"name":@"额外的请求參数"};

//
使用AFHTTPRequestOperationManager发送POST请求

[appDelegate.manager

POST:@"http://192.168.1.88:8888/AFNetworkingServer/upload"

parameters:parameters

//使用代码块来封装要上传的文件数据

constructingBodyWithBlock:^(id<AFMultipartFormData>
formData)

{

[formDataappendPartWithFileURL:filePath //指定上传的文件

name:@"file" //指定上传文件相应的请求參数名

//指定上传文件的原始文件名称

fileName:[NSStringstringWithFormat:@"%@.png"
,fileName]

//指定上传文件的MIME类型

mimeType:@"image/png"

error:nil];

}

//获取server响应成功时激发的代码块

success:^(AFHTTPRequestOperation *operation,id
responseObject)

{

//当使用HTTP响应解析器时,server响应数据被封装在NSData中

//
此处将NSData转换成NSString、并使用UIAlertView显示登录结果

[[[UIAlertViewalloc]initWithTitle:@"上传结果"
message:

[[NSStringalloc]initWithData:responseObjectencoding:

NSUTF8StringEncoding]delegate:self

cancelButtonTitle:@"确定"otherButtonTitles:nil]

show];

}

//获取server响应失败时激发的代码块

failure:^(AFHTTPRequestOperation *operation,NSError
*error)

{

NSLog(@"获取server响应出错!

");

}];

}

@end

源代码下载: http://download.csdn.net/detail/wangliang198901/7813361

注:本文章属于个人原创  请尊重个人劳动成果,谢谢。

版权声明:本文博主原创文章,博客,未经同意,不得转载。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905