gpt4 book ai didi

iphone - ASIHTTP : Upload UIImage?

转载 作者:太空狗 更新时间:2023-10-30 03:40:32 25 4
gpt4 key购买 nike

谁能告诉我如何在 Objective-c 中使用 ASIHTTPRequest 对象来上传 UIImage 对象?我需要将它转换为 NSData 对象吗?

(这是头像上传地址)

E.g. 

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

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

最佳答案

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

    //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"];

关于iphone - ASIHTTP : Upload UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4770949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com