zoukankan      html  css  js  c++  java
  • IOS上传图片的方法

    下面是图片上传的方法:

    -(void)loadImage:(NSString*)aurl
    {
            NSData              *imageData;
            NSMutableData       *postBody;
            NSString            *stringBoundary, *contentType;
            NSURL *url = [NSURL URLWithString:aurl];  //将字符串转换为NSURL格式
           
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"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];
    }
  • 相关阅读:
    密钥学习
    MAP的计算方法(简单总结)
    模型量化技术(入门级理解,不涉及复杂公式和深入的原理)
    实现java非阻塞http请求的两种方式
    PIP安装软件报错:“ERROR: Could not install packages due to an EnvironmentError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443)”
    Fiddler弱网测试
    Fiddler断点应用
    Fiddler基本介绍
    Fiddler安装及证书配置教程(Windows)
    URL统一资源定位符
  • 原文地址:https://www.cnblogs.com/qiqibo/p/2705543.html
Copyright © 2011-2022 走看看