0

約1年前に、写真や画像をネットワークに読み込むコードを書きました。しかし今、このコードは動作しません。私は今、私がログにIOS 10iOS 10のUIPikerViewController

を使用し、それはそれに接続されていると思う:[:1053969 34067]未知のタイプと画像フォーマットの作成

[一般的なこと]エラー

レポーターです応答{ コード= 5; msg = "ファイルが空でした"; }

どうかお手数ですが、どうしたらよいですか?

のInfo.plist enter image description here

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]; 
} 
+1

ピッカービューで写真をピンクにしたり、写真をアップロードできません。 – CodeChanger

+0

@CodeChangerはピッカービューから写真をピンク色にしません。 –

+0

@МарияТимофееваこれをチェックしましたか?http://stackoverflow.com/questions/38236723/ios-10-error-access-private-when-using-uiimagepickercontroller –

答えて

0

コードのベース&コメント画像をアップロードしている間にImageオブジェクトがリリースされると仮定します。コードの下

試してみてください。

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ 

    [picker dismissViewControllerAnimated:YES completion:NULL]; 

    [dataManager.arrOfTags replaceObjectAtIndex:0 withObject:@"Фото"]; 

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
    hud.mode = MBProgressHUDModeDeterminate; 
    NetManager *netManager = [NetManager new]; 

    UIImage *imgPhoto = [[UIImage alloc] initWithImage:info[UIImagePickerControllerOriginalImage]]; 

    [netManager uploadPhoto:imgPhoto 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]; 
} 

はUIImageの新しいオブジェクトを作成し、アップロード方法にそれを渡すようにしてください。

希望すると、これが役立ちます。

+0

ありがとう、しかし私は試して、それは動作していない –