美文网首页
iOS 阿里云图片上传

iOS 阿里云图片上传

作者: iOS小武哥 | 来源:发表于2019-02-12 10:29 被阅读14次
直接上代码:

阿里云上传图片,首先先从后台获取AccessKeyId AccessKeySecret SecurityToken 用这三个值调阿里云上传图片API

1.首先我们新建一个类:AliyunOSSUpload 继承NSObject

.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#define AliyunUpload                [AliyunOSSUpload aliyunInit]

@interface AliyunOSSUpload : NSObject

+(AliyunOSSUpload *)aliyunInit;
/*
imgArr 图片数组
imagesObject 图片名称(需要和后台商量图片名称的格式)
*/
-(void)uploadImage:(NSArray*)imgArr imagesObject:(NSArray *)imagesObject success:(void (^)(NSString *obj))success;
@end

.m

#import <AliyunOSSiOS/OSSService.h>
#import <AliyunOSSiOS/OSSCompat.h>
#import "AliyunOSSUpload.h"
#import "IHUtility.h" //就是对图片名字的操作
#import "NSObject+SBJSON.h"

NSString * const endPoint = @"";

OSSClient * client;
@implementation AliyunOSSUpload
static AliyunOSSUpload *_config;

+(AliyunOSSUpload *)aliyunInit{
    @synchronized(self){
        if (_config==nil) {
            [OSSLog enableLog];
            _config=[[AliyunOSSUpload alloc] init];
            
            id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:@"服务器获取" secretKeyId:@"服务器获取" securityToken:@"服务器获取"];
            client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential];
        }
    }
    return _config;
}



-(void)uploadImage:(NSArray*)imgArr imagesObject:(NSArray *)imagesObject success:(void (^)(NSString *obj))success{
    NSMutableArray *imgArray=[NSMutableArray new];
    for (int i=0; i<imgArr.count; i++) {
        NSData* data;

        UIImage *image1 = (UIImage *)imgArr[i];
        UIImage *image=[IHUtility rotateAndScaleImage:image1 maxResolution:(int)ScreenWidth*2];
        OSSPutObjectRequest * put = [OSSPutObjectRequest new];
        put.contentType=@"image/jpeg";
        put.bucketName = @"跟后台要";
        NSString *imgName;
            NSData *data1=UIImageJPEGRepresentation(image, 1);
            float length1 = [data1 length]/1024;
            if (length1<600) {
                data = UIImageJPEGRepresentation(image, 1);
            }else{
                data = UIImageJPEGRepresentation(image, 0.5);
            }
              //保证和服务器的文件名字一样
              imgName = imagesObject[i];
        put.objectKey = imgName;
        put.uploadingData = data; // 直接上传NSData
        
        put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
            NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
        };
        
        NSString *imgWidth;
        NSString *imgHeigh;
            imgWidth=[NSString stringWithFormat:@"%lf",image.size.width];
            imgHeigh=[NSString stringWithFormat:@"%lf",image.size.height];
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"/%@",imgName],@"t_url",
                           imgWidth,@"t_width",
                           imgHeigh,@"t_height",
                           nil];
        [imgArray addObject:dic];
        
        if (client==nil) {
            id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:@"服务器获取" secretKeyId:@"服务器获取" securityToken:@"服务器获取"];
            
            client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential];
        }
        
        OSSTask * putTask = [client putObject:put];
        
        [putTask continueWithBlock:^id(OSSTask *task) {
            if (!task.error) {
                NSLog(@"upload object success!");
                 if (type==ENT_fileImageProject){
                    if (i==imgArr.count-1) {
                        NSString *str=[imgArray JSONRepresentation];
                        success(str);
                    }
                }
                
            } else{
                NSLog(@"upload object failed, error: %@" , task.error);
                [SVProgressHUD showErrorWithStatus:@"图片上传失败!"];
            }
            return nil;
        }];
    }
}

@end

相关文章

  • 阿里云(I)

    参考资料:iOS 阿里云服务器图片上传iOS录制(或选择)视频,压缩、上传(整理)视频 拍摄 与压缩 阿里云OSS...

  • 图片上传

    图片上传 获取阿里云接口 图片上传到阿里云 图片预览接口post http://113.108.139.178:1...

  • 移动端实现图片压缩上传

    上传图片有很多框架,或者是阿里云直传,关于阿里云直传可以看我之前的博客上传图片到阿里云,这次是通过后台进行操作上传...

  • iOS阿里云多图片上传

    前提,阿里云文件上传文档已经了解过 我们在上传多图片的时候,通常会遇到如下问题: 1、图片如何有序的上传? 2、如...

  • iOS 使用阿里云上传图片

    前言: 项目要把图床从七牛迁到阿里云。因此,就要使用阿里云的图床上传。我去看了阿里云开发文档给出的示例代码和git...

  • 阿里云上传图片

    + (void)uploadImages:(NSArray *)images isAsync:(BOOL)isAs...

  • 阿里云上传图片

    引言 为了更好的掌握 OSS 存储,可以先去了解一下这些基本概念[https://help.aliyun.com/...

  • 封装直传阿里云存储文件上传控件

    本文目标 封装一个直传阿里云OSS云存储图片上传控件 控件效果图 上传之前 上传成功 预览图片 文件上传前后台及阿...

  • Android OSS上传图片到阿里云

    Android OSS上传图片到阿里云 我所采取的上传方式为异步上传,如果需要多张图片的话,可以直接循环上传就可...

  • IOS OSS-阿里云上传图片

    导入SDK,这部分就不介绍了 这里只介绍如果上传图片 阿里云的官方文档讲解的比较详细,但是坑爹的是少了行代码,所以...

网友评论

      本文标题:iOS 阿里云图片上传

      本文链接:https://www.haomeiwen.com/subject/ftyheqtx.html