首页 > 其他分享 >IOS上传图片的方法

IOS上传图片的方法

时间:2023-04-30 18:33:26浏览次数:42  
标签:postBody NSUTF8StringEncoding stringBoundary IOS dataUsingEncoding NSString appe


下面是图片上传的方法:

-(void)loadImage:(NSString*)aurl
{
        NSData                    *imageData;
        NSMutableData        *postBody;
        NSString                *stringBoundary, *contentType;
        NSURL *url = [NSURL URLWithString:aurl];    //将字符串转换为NSURL格式
       
    NSArray *paths = NSSearchPathForDirectori esInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathCom ponent:@"1.jpg"];

    imageData = [[NSData alloc] initWithContentsOfFile:path];

        ASIFormDataRequest *aRequest = [[ASIFormDataRequest alloc] initWithURL:url];
       
        stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
       
    contentType    = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];

        [aRequest addRequestHeader:@"Content-Type" value:contentType];
       
        postBody = [[NSMutableData alloc] init];
       
    [postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
       
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="title"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
       
    [postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]];    // So Light Table show up as source in Twitter post
       
    NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
       
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
       
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="upload"; filename="%@"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
       
    [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
       
    [postBody appendData:imageData];
       
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
       
        [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="password"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
       
    [postBody appendData:[[NSString stringWithString:@"lighttablexxxxxxxx"] dataUsingEncoding:NSUTF8StringEncoding]];   
       
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [aRequest setDelegate:self];
   
        [aRequest appendPostData:postBody];
   
    [aRequest setRequestMethod:@"POST"];   
       
    [aRequest startAsynchronous];
       
        [postBody release];
    [aRequest release];
}

标签:postBody,NSUTF8StringEncoding,stringBoundary,IOS,dataUsingEncoding,NSString,appe
From: https://blog.51cto.com/u_548275/6238007

相关文章

  • iOS应用检查更新的最优方法
    虽然AppStore能够提醒用户软件更新,但很多用户不会主动去更新一大堆的软件(我机器上待更新的软件在100个左右)。如果在软件开启时提醒用户更新或者更多中设置“检查更新”项,效果会好很多。1.NSString*version=@"";2.NSURL*url=[NSURLURLWithString:@"http://itunes.a......
  • IOS 小技巧积累
    1.使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。   直接调用成员变量并且给其赋值:member=[NSStringstringWithFormat:@””];将不执行setmember方法。  使用self调用成员变量并且给其赋值:self.member=......
  • Android裁剪图片为圆形图片
    以前在论坛中找过裁剪图片为圆形图片的方法,但是效果都不是很理想,这几天因为公司业务的要求,需要对头像进行裁剪以圆形的方式显示,这个方法是根据传入的图片的高度(height)和宽度(width)决定的,如果是width<=height时,则会裁剪高度,裁剪的区域是宽度不变高度从顶部到宽度width的长度;如果......
  • 用ChatGPT生成图片的指令
    接下来我会给你指令,生成相应的图片,我希望你用Markdown语言生成,不要用反引号,不要用代码框,你需要用UnsplashAPI,遵循以下的格式:source.unsplash.com/1600x900/?<将您的查询放在此处>。你明白了吗? ......
  • 博客园图片缩放调整
    上传图片后把![image](https://img2023.cnblogs.com/blog/2421910/202304/2421910-20230429223423752-834545576.png)改为<imgsrc="https://img2023.cnblogs.com/blog/2421910/202304/2421910-20230429223423752-834545576.png"style="zoom:50%"alt=&qu......
  • iOS百度地图 定位的实现
    今天发现自己好笨啊。。。一直在搞定位,想为什么会是空的,原来定位也是需要代理去实现的在初始化地图的时候,也就是在viewDidload里面是这样的mapView.showsUserLocation=YES;if(mapView.userLocation.location!=nil){NSLog(@"定位成功");coor=......
  • IOS为每个网请求添加进度标示
    UIViewController的子类,且只有界面才有必要现实进度标示。然后在网络请求的开始和结束调用这个方法1.基于UIViewController做扩展#import@implementationvoid)showProgress{NSLog(@"=======showProgress=========");MBProgressHUD*progressHUD=[[[MBPro......
  • [深入浅出Cocoa]iOS网络编程之Sock…
    一,iOS网络编程层次模型在前文《深入浅出Cocoa之Bonjour网络编程》中我介绍了如何在Mac系统下进行Bonjour编程,在那篇文章中也介绍过Cocoa中网络编程层次结构分为三层,虽然那篇演示的是Mac系统的例子,其实对iOS系统来说也是一样的。iOS网络编程层次结构也分为三层:Cocoa层:NSURL......
  • [iOS]GCD小结
    BriefIntroduction  GCD,全称GrandCentralDispath,是苹果开发的一种支持并行操作的机制。它的主要部件是一个FIFO队列和一个线程池,前者用来添加任务,后者用来执行任务。  GCD中的FIFO队列称为dispatchqueue,它可以保证先进来的任务先得到执行(但不保证一定先执行结束)。 ......
  • IOS实例变量和属性
    在IOS编程中,很多人没有理解实例变量与属性的区别,我根据自己的经验,这这里与大家分享下这里面的区别,下面我写一个简单的例子:.h@interfaceMyController:UIViewController{UILabel*_myLabel;//这里是实例变量声明}@property(nonatomic,retain)UILabel*myLabe......