gpt4 book ai didi

ios - 使用 AFNetworking 2.0 加载图像

转载 作者:可可西里 更新时间:2023-11-01 03:31:44 27 4
gpt4 key购买 nike

我正在尝试使用 AFNetworking 2.0 将照片添加到 POST。此 ios 应用程序将帖子和照片发送到博客。我不明白为什么图片无法加载。

这是我到目前为止得到的:

// publish text and image
-(void)publishTextAndImage:(NSString*)resultDisplay and:(NSString*)subject with:(NSString*)nonce
{
imageData = UIImageJPEGRepresentation(selectedImage, 0.7); // create a data object from selected image

NSString *myUUID = [[NSUUID UUID] UUIDString]; // create a UUID
NSString *formatString = [NSString stringWithFormat:@"<img src=\"/wp-content/uploads/%@\"/>",myUUID];
NSString *contentString = [formatString stringByAppendingString:resultDisplay];
NSString *moodString = [NSString stringWithFormat:@"%d",self.moodNumber];

NSDictionary *parameters = @{@"title":subject,
@"content":contentString,
@"status":@"publish",
@"author":@"wordpress",
@"user_password":@"xrayyankee",
@"nonce":nonce,
@"categories":moodString,
@"attachment":@"image/jpeg"};

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager POST:@"http://thrills.it/?json=posts/create_post"
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if (selectedImage)
{
[formData appendPartWithFileData:imageData name:@"photo" fileName:myUUID mimeType:@"image/jpeg"];
}

}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);

}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];

非常感谢

最佳答案

我以这种方式使用 AFNetworking:

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://thrills.it/?json=posts"]];
NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"create_post" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if (selectedImage)
{
[formData appendPartWithFileData:imageData name:@"photo" fileName:myUUID mimeType:@"image/jpeg"];
}

} ];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Error: %@", error);
}];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float progressValue = (float)((double)totalBytesWritten/(double)totalBytesExpectedToWrite);
NSLog(@"%f", progressValue);
}];

[self.queue addOperation:operation];

.

@property (nonatomic, strong) NSOperationQueue *queue;

我的客户端是较早创建的,但它是那样创建的。

希望对您有所帮助。

关于ios - 使用 AFNetworking 2.0 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21789169/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com