iOS-七牛云上传图片
2017-02-23 本文已影响1009人
青巷水彩
#import "AddImgsViewController.h"
#import "ImagePicker.h"
#import "UploadImageManager.h"
#import "NetworkManager.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "QNUploadManager.h"
#import "QiniuSDK.h"
#import "NetworkManager.h"
@interface AddImgsViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (nonatomic, strong) NSString *token;
@end
@implementation AddImgsViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadSenvenToken];
}
- (void)loadSenvenToken{
NSString *url = [NSString stringWithFormat:@"%@%@",URL_Base,URL_CommentsToken];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *apikey = [NSString stringWithFormat:@"%@",[user objectForKey:@"apikey"]];
[NetworkManager requestGETWithURLStr:url paramDic:nil Api_key:apikey finish:^(id responseObject) {
NSLog(@"token请求成功%@",responseObject);
self.token = [responseObject objectForKey:@"token"];
} enError:^(NSError *error) {
NSLog(@"失败%@",error);
}];
}
- (IBAction)addImgBtn:(id)sender {
[self gotoImageLibrary];
}
- (IBAction)uploadAction:(UIButton *)sender {
if (self.img.image == nil) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"还未选择图片"
message:@""
delegate:nil
cancelButtonTitle:@"OK!"
otherButtonTitles:nil];
[alert show];
} else {
[self uploadImageToQNFilePath:[self getImagePath:self.img.image]];
}
}
- (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];
}
- (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.img.image = image; //imageView为自己定义的UIImageView
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
//照片获取本地路径转换
- (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;
}