七牛云储存理解

1.获取用户编辑之后的图片

 if (editedImage) {
        photoPath = [CacheUtil cachePhoto:editedImage];
        [self uploadPhoto];
    }

2.通过缓存工具取出路径
2.1获取缓存文件的路径

 `+ (NSString *)fileDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString * path = [NSString stringWithFormat:@"/%@",@"photoCache"];
    NSString * fileDirectory = [documentDirectory stringByAppendingPathComponent:path];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:fileDirectory]) {
        [fileManager createDirectoryAtPath:fileDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return fileDirectory;
}`

2.2
新建一个图片的路径,将图片压缩0.5 之后存进一个nsdata 里面。
将这个data写道指定路径的位置,返回这个路径。

+ (NSString *)cachePhoto:(UIImage *)photo{
    NSString * photoPath = [[self fileDirectory ] stringByAppendingPathComponent:[photo imageDataMd5]];
    //具体这段代码的意义需要再考证
    NSData * data = UIImageJPEGRepresentation(photo, 0.5);
    [data writeToFile:photoPath atomically:YES];
    data = nil;
    return photoPath;
}

3.上传这个路径下的文件
3.1 首先从服务端获取token凭证

- (void)uploadFilePath:(NSString *)filePath formatType:(NSString *)formatType sender:(CompletionHandler)handler{
    requestHander = handler;
    [self requestToken];
    m_filePath = filePath;
    m_formatType = formatType;
}

3.2
请求token之后执行上传

- (void)requestToken{
    [AFHttpTool getTokenSuccess:^(AFHTTPRequestOperation *operation, id response) {
        token = response[@"token"];
        [self uploadFile];
        //上传
    } failure:^(id err) {
        AFHTTPRequestOperation * op = err;
        NSDictionary * response = [op.responseString jsonValue];
        if (response[@"error"][@"message"]) {
            [[[UIAlertView alloc] initWithTitle:nil message:response[@"error"][@"message"] delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil] show];
        }

    }];
}

3.3 上传图片到七牛

- (void)uploadFile{
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    NSData* data = [NSData dataWithContentsOfFile:m_filePath];

    NSArray * tempQinniuStr = [m_filePath componentsSeparatedByString:@"/"];
    //将上传过来的路径名分割成数组
    //取出最后一个元素作为上传时候的key(相当于在七牛里面的唯一ID);

    NSString * qiniuFilePath2 = [NSString stringWithFormat:@"%@",[tempQinniuStr lastObject]];

    [upManager putData:data key:qiniuFilePath2 token:token
              complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                  NSLog(@"%@", info);
                  NSLog(@"%@", key);
                  qiniuFilePath = [NSString stringWithFormat:@"%@%@",kQiNiuDomain,key];
                  requestHander(YES, qiniuFilePath);
                  //上传成功block返回七牛里面的路径
              } option:nil];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值