以前上传图片都是通过form表单提交上传文件类型数据,这次的后台有点逗逼用起了第三方,我也是呵呵了,因此找了一下文档,第三方SDK集成我就不多说了,毕竟都是按照人家的步骤来就行了。
因此,首先登录七牛云,然后找到官方SDK,找到Objective-C文档,点击一下就可以进到文档界面,如下:
一个是SDK的下载地址,一个是七牛云的demo(跳转到GitHub界面下载demo),文档中对于自自动导入使用Pod带上版本号;手动导入也有需要添加的库。配置照着文档来应该不成问题,主要说说如何使用
1、首先从媒体库或者拍照完成后选择图片
//选择图片
- (IBAction)chooseAction:(id)sender {
[self gotoImageLibrary];
}
- (void)gotoImageLibrary {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"访问图片库错误"
message:@""
delegate:nil
cancelButtonTitle:@"OK!"
otherButtonTitles:nil];
[alert show];
}
}
//再调用以下委托:
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
self.pickImage = image; //imageView为自己定义的UIImageView
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
2、选择完图片之后上传图片
//上传图片
- (IBAction)uploadAction:(id)sender {
if (self.pickImage == nil) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"还未选择图片"
message:@""
delegate:nil
cancelButtonTitle:@"OK!"
otherButtonTitles:nil];
[alert show];
} else {
[self uploadImageToQNFilePath:[self getImagePath:self.pickImage]];
}
}
- (void)uploadImageToQNFilePath:(NSString *)filePath {
self.token = @"你的token";
QNUploadManager *upManager = [[QNUploadManager alloc] init];
QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
NSLog(@"percent == %.2f", percent);
}
params:nil
checkCrc:NO
cancellationSignal:nil];
[upManager putFile:filePath key:nil token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"info ===== %@", info);
NSLog(@"resp ===== %@", resp);
}
option:uploadOption];
}
//照片获取本地路径转换
- (NSString *)getImagePath:(UIImage *)Image {
NSString *filePath = nil;
NSData *data = nil;
if (UIImagePNGRepresentation(Image) == nil) {
data = UIImageJPEGRepresentation(Image, 1.0);
} else {
data = UIImagePNGRepresentation(Image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *ImagePath = [[NSString alloc] initWithFormat:@"/theFirstImage.png"];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:ImagePath] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc] initWithFormat:@"%@%@", DocumentsPath, ImagePath];
return filePath;
}
在这需要三个参数:
(1)filePath:要上传的文件路径
(2)key:指定到七牛服务器上的文件名,可以为nil(必须以UTF-8编码的字符串)
(3)token:从服务端SKDK获取(后台写一个接口返回)
这里需要注意两点:
(1)key:上传到七牛服务器的文件名,这个之前博主一直使用的是后台返回的key,最后发现到资源文件中只有一张图片,因为使用的都是一个唯一的key,因此文件名一样文件总是被替换覆盖
(2)上传成功与失败
成功时,会返回一个status=200,error返回null,且resp中还会返回一个hash和key
失败时,会具体报错,根据相应的错误去找根源即可