调用
AliyunOSSUpload *up = [[AliyunOSSUpload alloc]init];
up.accessKey = @"后台返回";
up.token = @"后台返回";
up.secretKey = @"后台返回";
NSString *str = [JXTools br_getDateString:[NSDate date] format:@"HHmmss"];
[up uploadImage:self.headerImg.image imagesObject:[NSString stringWithFormat:@"%@1.png",str] success:^(NSString * _Nonnull obj) {
[self.headerImg sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:nil];
}];
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface AliyunOSSUpload : NSObject
/** endpoint */
@property(nonatomic,strong)NSString *endpoint;
/** accessKey */
@property(nonatomic,strong)NSString *accessKey;
/** secretKey */
@property(nonatomic,strong)NSString *secretKey;
@property(nonatomic,strong)NSString *token;
/** bucketName */
@property(nonatomic,strong)NSString *bucketName;
/*
imgArr 图片
imagesObject 图片名称(需要和后台商量图片名称的格式)
*/
//单图上传
-(void)uploadImage:(UIImage*)img imagesObject:(NSString *)imagesObject success:(void (^)(NSString *obj))success;
@end
NS_ASSUME_NONNULL_END
#import "AliyunOSSUpload.h"
#import <AliyunOSSiOS/OSSService.h>
#define AliyunImgUrl @""
@interface AliyunOSSUpload ()
/** OSSClient */
@property(nonatomic,strong)OSSClient *client;
@end
@implementation AliyunOSSUpload
- (id)init {
if (self = [super init]) {
//默认参数:(必传)
self.endpoint = @"";
self.bucketName = @"问自己的后台";
}
return self;
}
-(void)uploadImage:(UIImage *)img imagesObject:(NSString *)imagesObject success:(void (^)(NSString * _Nonnull))success{
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.contentType=@"image/jpeg";
put.bucketName = self.bucketName;
put.objectKey = [self objectKey:imagesObject];
put.uploadingFileURL = [NSURL fileURLWithPath:[self getImagePath:img]];
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
// 移动端建议使用STS方式初始化OSSClient。
id<OSSCredentialProvider> credential = [[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * _Nullable{
OSSFederationToken *token = [OSSFederationToken new];
// 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
token.tAccessKey = self.accessKey;
token.tSecretKey = self.secretKey;
// 从STS服务获取的安全令牌(SecurityToken)。
token.tToken = self.token;
return token;
}];
self.client = [[OSSClient alloc] initWithEndpoint:self.endpoint credentialProvider:credential];
OSSTask * putTask = [self.client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"upload object success!");
NSString *imgstr = [NSString stringWithFormat:@"%@%@",AliyunImgUrl,[self objectKey:imagesObject]];
success(imgstr);
} else{
NSLog(@"upload object failed, error: %@" , task.error);
[JXTools showAlertWithMessage:@"图片上传失败!"];
}
return nil;
}];
}
-(NSString *)objectKey:(NSString *)imagesObject{
NSString *str = [JXTools br_getDateString:[NSDate date] format:@"yyyy/MM/dd"];
NSString *objectKey = [NSString stringWithFormat:@"ios/test/%@/%@",str,imagesObject];
return objectKey;
}
//照片获取本地路径转换
- (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:@"/jxImage.png"];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:ImagePath] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc] initWithFormat:@"%@%@", DocumentsPath, ImagePath];
return filePath;
}
@end
网友评论