ios开发上传图片,ios上传图片到服务器

iOS原生上传图片方案

(1)方式一

公司主营业务:成都网站建设、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出曲周免费做网站回馈大家。

(2)方式二

请求头 (图片来自Charles 4.5.6)

IOS开发,上传完图片后,怎么获取图片的URL

移动端在图片上传完成后可以让服务端将上传成功的图片的url返回给你

iOS上传图片大小保持在1M以内

实际开发过程中很有可能会遇到一些比较麻烦的需求:这里就分享下上传图片的时候限制上传的图片的大小的问题,调用系统相册以及调用照相机的实现我就不详细说了,这里主要说一下压缩图片至1M以内的实现,顺带着将file进行base64转码的方法说一下,以下进入主题:

// 选中照片之后的回调方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info

{

// 先dismiss调选取照片的VC,在completion block里面完成以下几个操作:

[self dismissViewControllerAnimated:YES completion:^{

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

// 拍照存入相册

if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

}

// 判断哪个视图需要照片

UIImage *tempImage = nil;

NSString *imageName = nil;

// 这里注意下:如果有多个上传图片的选项,可以给每个选项设置一个类型,然后每个选项根据相应的类型来设置不同的imageName即可.暂时我只设置一个值,如@"test.jpeg".

imageName = @"test.jpeg";

// 这里:如果有多个上传图片的选项,且给每个选项设置一个类型,这时就可以根据imageName的值的不同,写入本地之后返回的image值也就不同..

// 将返回的本地的图片赋值给tempImage

tempImage = [self writeToCachesWithImage:image imageName:imageName];

// 下面就是调用你自己的上传图片的网络请求了,这是我的网络请求封装的方法....

[self uploadImageWithImage:tempImage imageName:imageName];

}];

}

// 把上传需要的图片转为字符串

- (NSData *)stringWithImage:(UIImage *)image imageName:(NSString *)imageName

{

NSString *imagePath = [[self postImagePathWithFileName:certificationFileName] stringByAppendingPathComponent:imageName];

NSData *imageData = nil;

CGFloat mb = 1.0; // 1MB

CGFloat uploadMB = 1.0; // 1M

CGFloat itemMB = 0; // 把文件转成MB

if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {

long long imageSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:imagePath error:nil] fileSize];

itemMB = imageSize / (mb * 1024 * 1024) ;

} else {

CGImageRef cgImageRef = image.CGImage;

size_t bpp = CGImageGetBitsPerPixel(cgImageRef);

size_t bpc = CGImageGetBitsPerComponent(cgImageRef);

size_t bytes_per_pixel = bpp / bpc;

CGFloat lPixelsPerMB = mb / bytes_per_pixel;

CGFloat totalPixel = CGImageGetWidth(cgImageRef) * CGImageGetHeight(cgImageRef);

itemMB = totalPixel / lPixelsPerMB;

}

if (itemMB = uploadMB) {

imageData = UIImageJPEGRepresentation(image, 1.0f);

} else {

// 大于1M压到1M

CGSize imageSize = image.size;

CGSize toImageSize = CGSizeZero;

if (imageSize.width imageSize.height) {

toImageSize = CGSizeMake(1024, 1024 * imageSize.height / imageSize.width);

} else if (imageSize.width imageSize.height) {

toImageSize = CGSizeMake(1024 * imageSize.width / imageSize.height, 1024);

} else {

toImageSize = CGSizeMake(sqrt(1048576), sqrt(1048576));

}

UIImage *toImage = [image imageCompressForSize:image targetSize:toImageSize];

imageData = UIImageJPEGRepresentation(toImage, 1.f);

}

// 这里注意下:根据你们自己的需要转成不同类型的NSData字节流...

// 这是不用base64转码返回的string

return imageData;

// 这是用base64转码返回的string,1.0上传的方式

//    Byte *byte = (Byte *)[imageData bytes];

//    return [NSString stringWithFormat:@"jpeg@%@", [GTMBase64 stringByEncodingBytes:byte length:imageData.length]];

}

/* 上传的图片所在的沙盒文件夹

* @param : fileName 文件的名称.

* @param : return 文件的存储路径

*/

- (NSString *)postImagePathWithFileName:(NSString *)fileName

{

NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:fileName];

if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {

[[NSFileManager defaultManager] createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];

}

return imagePath;

}

/*

* 把图片存入本地并返回

* @param : image 选中的image

* @param : imageName 图片的名称

* @return : UIImage

*/

- (UIImage *)writeToCachesWithImage:(UIImage *)image imageName:(NSString *)imageName

{

NSString *imagePath = [[self postImagePathWithFileName:certificationFileName] stringByAppendingPathComponent:imageName];

BOOL isResult = [UIImageJPEGRepresentation(image, 1.0f) writeToFile:imagePath atomically:YES];

if (isResult) {

// 写入成功,把图片取出来

UIImage *certificationImage = [UIImage imageWithContentsOfFile:imagePath];

return certificationImage;

} else {

// 写入失败,直接返回原始图片

return image;

}

}

ios用asi怎么上传系统相册照片

1.新建一个single view工程,导入ASIHttpRequest库,导入MobileCoreServices、CFNetwork、SystemConfiguration和libz1.2.5.dylib四个系统库

2.随便导入一张图片,比如haoyou.png

3.ViewController.h

#import uikit uikit.h=""

#import "ASIHTTPRequest.h"

#import "ASIFormDataRequest.h"

@interface ViewController : UIViewController

@property (nonatomic, copy)NSString *m_auth;

@end/asihttprequestdelegate/uikit

4.ViewController.m 添加两个按钮

(void)viewDidLoad {

[super viewDidLoad];

UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

loginBtn.frame = CGRectMake(100, 20, 120, 40);

[loginBtn setTitle:@"登录" forState:UIControlStateNormal];

[loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:loginBtn];

UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

uploadBtn.frame = CGRectMake(100, 80, 120, 40);

[uploadBtn setTitle:@"上传" forState:UIControlStateNormal];

[uploadBtn addTarget:self action:@selector(upload) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:uploadBtn];

}

5.实现login和upload两个方法

- (void)login {

NSURL *url = [NSURL URLWithString:@"..."];//此处省略请求url

//请求

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

request.tag = 10;

request.delegate = self;

[request startAsynchronous];

}

- (void)upload {

NSURL* url = [NSURL URLWithString:@"..."];//此处省略请求url

UIImage* img = [UIImage imageNamed:@"haoyou.png"];

NSData* data = UIImagePNGRepresentation(img);

//ASIFormDataRequest请求是post请求,可以查看其源码

ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:url];

request.tag = 20;

request.delegate = self;

[request setPostValue:self.m_auth forKey:@"m_auth"];

// [request setFile:@"tabbar.png" forKey:@"haoyou"];//如果有路径,上传文件

[request setData:data withFileName:@"tmp.png" andContentType:@"image/png" forKey:@"headimage"];

// 数据 文件名,随便起 文件类型 设置key

[request startAsynchronous];

}

6.实现协议

- (void)requestFailed:(ASIHTTPRequest *)request {

NSLog(@"请求失败");

}

- (void)requestFinished:(ASIHTTPRequest *)request {

if (request.tag == 10) {

NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:request.responseData options:0 error:nil];

self.m_auth = [dic objectForKey:@"m_auth"];

NSLog(@"%@", self.m_auth);

}

if (request.tag == 20) {

NSLog(@"%@", request.responseString);

}

}


新闻名称:ios开发上传图片,ios上传图片到服务器
标题路径:http://ybzwz.com/article/phigcc.html