iOS 阿里云图片上传

直接上代码:

阿里云上传图片,首先先从后台获取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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 因为端午节到了,所以放了三天假,这三天假,我去了外婆家。 去外婆家的第一天,我们上午从家里出发,下午到...
    木子李4255558阅读 330评论 1 1
  • 我老婆某表姐的婚姻亮了红灯,近来,老婆一有时间就跟我讲那小三有多可恶,那表姐夫也有多可恶,确实听起来可恶至极,听说...
    一句话说完阅读 4,250评论 49 94
  • js的原始类型(primitive type)有以下五种类型:Undefined,Null,Boolean,Num...
    青春前行阅读 322评论 0 0
  • 我特别不想去承认一点,我还是对你念念不忘,可是真的好多时候我都想起你了,很多时刻都有想过要和你一起分享。 但是没有...
    琨没有鹏阅读 230评论 0 1

友情链接更多精彩内容