0

J'ai écrit du code il y a environ un an, où je charge une photo ou une image sur le réseau. Mais maintenant ce code ne fonctionne pas. Je pense qu'il est connecté à ce moment j'utilise IOS 10.UIPikerViewController pour ios 10

Dans les journaux:

[générique] Création d'un format d'image avec un type inconnu est une erreur

Reporter [34067: 1053969] Réponse { code = 5; msg = "Le fichier était vide"; }

Aidez-moi, quel est le problème?

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

Qu'est-ce qui ne fonctionne pas en train de piquer la photo depuis la vue du sélecteur ou de télécharger la photo ne fonctionne pas? – CodeChanger

+0

@CodeChanger ne bloque pas la photo de la vue du sélecteur. –

+0

@ МарияТимофеева Avez-vous vérifié celui-ci http://stackoverflow.com/questions/38236723/ios-10-error-access-private-when-using-uiimagepickercontroller –

Répondre

0

Base sur votre code & commentaires Je suppose que l'objet Image sera publié pendant le téléchargement de votre image.

Essayez ci-dessous le code:

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

Essayez de créer nouvel objet de UIImage et passe dans votre méthode de téléchargement.

J'espère que cela vous aidera.

+0

merci, mais j'essaie, ça ne fonctionne pas –