图片转换为base64字符串作为参数上传

2018-04-26  本文已影响0人  芭里克婷

1.接口要求:接口的上传方式是post,带参数。一个是strbase64,要求图片转化为base64字符串,另一个"type":p,没太大用处。我开始一脸蒙蔽,感谢我滴朋友不吝赐教。

2.我的代码是

- (NSString*)imageToString:(UIImage*)image {

    NSData*imagedata =UIImagePNGRepresentation(image);

    NSString *image64 = [imagedata base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    returnimage64;

}

-(void)uploadPhotos:(UIImage*)image{

    NSString*image64 = [selfimageToString:image];

    NSString *url=[NSString stringWithFormat:@"%@%@",HTTP, @""];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];

    NSDictionary*dict =@{@"strbase64":image64,@"type":@"p"};

    [managerPOST:urlparameters:dictprogress:^(NSProgress*_NonnulluploadProgress) {

    }success:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        NSLog(@"上传证件照成功了");

        NSLog(@"证件照成功返回式数据%@",responseObject);

    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

        NSLog(@"上传证件照失败了");

    }];

}

上一篇下一篇

猜你喜欢

热点阅读