ASIHTTP:上传UIImage? [英] ASIHTTP: Upload UIImage?

查看:111
本文介绍了ASIHTTP:上传UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何在Objective-c中使用ASIHTTPRequest对象上传UIImage对象吗?我需要将其转换为NSData对象吗?

Can someone please tell me how I can use an ASIHTTPRequest object in Objective-c to upload an UIImage object? Do I need to convert it to an NSData object?

(这是一个头像上传网址)

(This is for an avatar upload url)

E.g. 

UIImage *toUpload = [UIImage imageNamed:@"test.jpg"]

URL: "http://www.example.com/api/users/avatar/upload?access_token=12345"
RequestType: PUT


推荐答案

<下面是一个使用ASIFormRequest的例子,它也会尝试将图像压缩到给定的最大尺寸

Heres an example using ASIFormRequest that will also attempt to compress the image to given maximum size

    //Compress the image
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 250*1024;

NSData *imageData = UIImageJPEGRepresentation(yourImage, compression);

while ([imageData length] > maxFileSize && compression > maxCompression)
{
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation(yourImage, compression);
}

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:whereYourServerEndPointIs.com]];

[request addData:imageData withFileName:@"someFileName.jpeg" andContentType:@"image/jpeg" forKey:@"uploadedImage"];

这篇关于ASIHTTP:上传UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆