約 1 年前に、写真や画像をネットワークにロードするコードを書きました。しかし今、このコードは機能しません。iOS10を使うようになったのも、それと関係があると思います。
ログ内:
[汎用] タイプが不明な画像フォーマットを作成するとエラーになる
Reporter[34067:1053969] 応答 { コード = 5; msg = "ファイルが空でした"; }
助けてください、何が問題なのですか?
Info.plist ここに画像の説明を入力
AddComplaintController.h
@interface AddComplaintController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate , UITextViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *photoButton;
@property (nonatomic, strong) UIImage *photo;
@property (nonatomic, strong) NSString *photoUrl;
...different properties...
@end
AddComplaintController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *photoButton = [[UIButton alloc] initWithFrame:CGRectMake(16, 11, 28, 26)];
[photoButton setImage:[UIImage imageNamed:@"photo"] forState: UIControlStateNormal];
[photoButton addTarget:self action:@selector(addPhoto:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)addPhoto:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
[self.view endEditing:YES];
[actionSheet bk_addButtonWithTitle:@"Все фотографии" handler:^{
[self selectPhoto];
}];
[actionSheet bk_addButtonWithTitle:@"Камера" handler:^{
[self takePhoto];
}];
[actionSheet bk_setCancelButtonWithTitle:@"Отмена" handler:^{
[self.text becomeFirstResponder];
}];
[actionSheet showInView:self.view];
}
- (IBAction)takePhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Device has no camera" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[myAlertView show];
[self.text becomeFirstResponder];
}
else{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (IBAction)selectPhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
[picker dismissViewControllerAnimated:YES completion:NULL];
picker.allowsEditing = YES;
[dataManager.arrOfTags replaceObjectAtIndex:0 withObject:@"Фото"];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminate;
NetManager *netManager = [NetManager new];
[netManager uploadPhoto:info[UIImagePickerControllerOriginalImage] andCallbackBlock:^(NSString *imageURL, double progress){
if (progress <= 1.0 && progress > 0) {
hud.progress = progress;
}
else if (progress == 0.0) {
self.photoUrl = imageURL;
[hud hide:YES];
NSLog(@"photo url %@", self.photoUrl);
if (self.photoUrl && !self.photoIsSticked) {
UIImageView *stick = [[UIImageView alloc] initWithFrame:CGRectMake(32, 8, 14, 14)];
stick.image = [UIImage imageNamed:@"tick"];
[keyboardToolbar addSubview:stick];
self.photoIsSticked = YES;
}
}
}];
[picker dismissViewControllerAnimated:YES completion:NULL];
[self.text becomeFirstResponder];
}
NetManager.m
- (void)uploadPhoto: (UIImage *) photo andCallbackBlock:(void (^)(NSString*, double))photoURL {
NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
NSString *urlString = [NSString stringWithFormat: @"%@api/v1/upload?token=%@", kBaseURLPath, token];
NSData *dataImage = UIImageJPEGRepresentation(photo, 0.5f);
NSDictionary *parms = @{@"command": @"upload",
@"title":@"foto"};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation = [manager POST:urlString parameters:parms constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:dataImage name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response %@", responseObject);
photoURL(responseObject[@"value"], 0.0);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite) {
NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
photoURL (nil, (double)totalBytesWritten/(double)totalBytesExpectedToWrite);
}];
[operation start];
}