You need to enable JavaScript to run this app.
导航

图片处理持久化(iOS SDK)

最近更新时间2024.02.04 18:31:09

首次发布时间2023.11.03 16:08:03

TOS 默认不保存处理后的图片。您可以使用代码将处理后的图片保存至指定的存储桶。

注意事项

  • 使用图片处理持久化的账号必须拥有原图所在桶的读权限,图片转存的目标桶的写权限。
  • 使用匿名用户将处理后的图片保存至指定的存储桶时,该存储桶必须为公共写权限。
  • 原图所在桶与图片转存的目标桶必须属于同一地域。
  • 转存图片的访问权限默认为私有,存储类型默认为标准存储。

示例代码

以下代码展示将图片缩放为高度 100px,然后将处理后的图片以指定对象名保存至目标存储桶。

// 从STS服务获取的临时访问密钥和安全令牌(AccessKey、SecretKey、SecurityToken)
TOSCredential *credential = [[TOSCredential alloc] initWithAccessKey:@"accesskey" secretKey:@"secretkey" securityToken:@"securityToken"];
TOSEndpoint *tosEndpoint = [[TOSEndpoint alloc] initWithURLString:@"endpoint" withRegion:@"region"];
TOSClientConfiguration *config = [[TOSClientConfiguration alloc] initWithEndpoint:tosEndpoint credential:credential];
TOSClient *client = [[TOSClient alloc] initWithConfiguration:config];

TOSGetObjectInput *get = [TOSGetObjectInput new];
get.tosBucket = @"bucket-name";
get.tosKey = @"image-name";
get.tosProcess = @"image/resize,h_100";
get.tosProcessSaveAsBucket = @"target-bucket-name";
get.tosProcessSaveAsObject = @"target-image-name";
TOSTask *task = [client getObject:get];
[task continueWithBlock:^id _Nullable(TOSTask * _Nonnull t) {
    if (!task.error) {
        NSLog(@"Get object to file success.");
        // 获取转存结果
        TOSGetObjectOutput *out = t.result;
        NSLog(@"%@", [[NSString alloc] initWithData:out.tosContent encoding:NSUTF8StringEncoding]);
    } else {
        NSLog(@"Get object to file failed, error: %@" ,task.error);
    }
    return nil;
}];

相关文档

关于图片处理持久化的详细介绍,请参见图片处理持久化