- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self.headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17*BiLiWidth, 16*BiLiWidth, 76*BiLiWidth, 76*BiLiWidth)];
self.headerImageView.contentMode = UIViewContentModeScaleAspectFill;
self.headerImageView.autoresizingMask = UIViewAutoresizingNone;
self.headerImageView.clipsToBounds = YES;
self.headerImageView.layer.cornerRadius = 76*BiLiWidth/2;
[self.contentView addSubview:self.headerImageView];
}
return self;
}
NSDictionary *headers = @{@"Content-Type": @"application/json"};
NSData *data = UIImagePNGRepresentation(self.originImage);
NSString * imageBase64Str = [data base64EncodedStringWithOptions:0];
NSDictionary * info = @{@"width":[NSNumber numberWithInt:100],@"top":[NSNumber numberWithInt:0],@"height":[NSNumber numberWithInt:100],@"left":[NSNumber numberWithInt:0]};
NSArray * rectangleArray = [[NSArray alloc] initWithObjects:info, nil];
//处理好的参数
NSDictionary * postDic = @{@"image":imageBase64Str,@"rectangle":rectangleArray};
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDic options:0 error:nil];
NSDictionary * tokenInfo = [GongJvManger getDefaultsObjectKey:ApiInfoDefaults];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?access_token=%@",@"https://aip.baidubce.com/rest/2.0/image-process/v1/inpainting",[tokenInfo objectForKey:@"access_token"]]]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[WSProgressHUD dismiss];
if (error) {
[WSProgressHUD showErrorWithStatus:error.localizedDescription];
} else {
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[dict allKeys] containsObject:@"error_code"]) {
NSNumber * errorCode = [dict objectForKey:@"error_code"];
if (errorCode.intValue==110||errorCode.intValue==111) {
[NetWorkModel getApiToken:@{@"grant_type":@"client_credentials",@"client_id":client_id,@"client_secret":client_secret} callback:^(NSInteger status, id _Nullable responseObject, NSString * _Nullable msg) {
[GongJvManger setDefaultsObject:responseObject forKey:ApiInfoDefaults];
}];
}
[WSProgressHUD showErrorWithStatus:[dict objectForKey:@"error_msg"]];
}
else
{
NSString * base64Str = [dict objectForKey:@"image"];
NSData * showData = [[NSData alloc]initWithBase64EncodedString:base64Str options:NSDataBase64DecodingIgnoreUnknownCharacters];
[self setFinishResultImage:[UIImage imageWithData:showData]];
}
}
});
}];
[dataTask resume];
|