在我的应用程序中,我允许我的用户从他们的照片库上传图片或拍照。拍摄(或选择)该照片后,我使用该 AssetURL 执行以下操作:
- (void)uploadImageWithAssetURL:(NSURL *)url named:(id)name withParameters:(NSDictionary *)parameters atPath:(NSString *)path
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:url resultBlock:^(ALAsset *asset )
{
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
NSString *extension = @".jpg";
NSString *mimeType = @"image/jpeg";
NSNumber *identifier = [parameters objectForKey:FileManagerOwnerObjectIDKey];
NSString *filename = [NSString stringWithFormat:@"%@_%@%@", name, [_dateFormatter stringFromDate:[NSDate date]], extension];
NSMutableURLRequest *request = [_httpClient multipartFormRequestWithMethod:@"POST" path:path parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:data name:@"file" fileName:filename mimeType:mimeType];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
[_delegate fileManagerDidSuccessfullyUploadPicture:[NSDictionary dictionaryWithObjectsAndKeys:identifier, FileManagerOwnerObjectIDKey, name, FileManagerOwnerObjectTypeKey, section, FileManagerOwnerObjectSectionKey, nil]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[_delegate fileManagerDidNotSuccessfullyUploadPicture:[NSDictionary dictionaryWithObjectsAndKeys:identifier, FileManagerOwnerObjectIDKey, name, FileManagerOwnerObjectTypeKey, section, FileManagerOwnerObjectSectionKey, nil]];
}];
[operation start];
}
failureBlock:^(NSError *error )
{
NSLog(@"Error loading asset");
}];
}
出于某种原因,如果图片是横向的,稍后会显示得很好。但是,如果该图片的良好方向是纵向,它看起来就像将其作为横向发送到我的 Django 服务器,并且我的服务器在调整大小等时将其视为横向。因此,我所有的肖像画都是颠倒的……
有任何想法吗?!