1、在工具类定义压缩图片的方法,传入图片和所要压缩的尺寸
1
2
3
4
5
6
7
8
9
10
11
12
13
| +(UIImage *)imageWithImage:(UIImage *)image scaleToSize :(CGSize )newSize{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
|
2、利用AFNetworking做图片上传功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| +(void)POSTToUrl:(NSString *)url WithData:(NSData *)data
WithReturnBlock:(ReturnValueBlock)block
WithErrorCodeBlock:(ErrorCodeBlock)errorBlock
WithFailtureBlock:(FailureBlock)failtureBlock{
AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc]init];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//不设置可能会出现404的bug
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:url parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//以上传的时间为图片名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
[formData appendPartWithFileData:data
name:@"upload" //upload.php处理文件的字段
fileName:fileName //图片名字
mimeType:@"image/png"];
}success:^(AFHTTPRequestOperation *operation,id responseObject) {
//上传成功
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
//上传失败
}
}
|