【无标题】

【无标题】,第1张

- (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];

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/996721.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存