【问题标题】:UIPikerViewController for ios 10ios 10 的 UIPikerViewController
【发布时间】:2016-12-12 12:06:53
【问题描述】:

大约一年前我编写了代码,将照片或图片加载到网络。但是现在这段代码不起作用。我认为现在我使用的是 IOS 10。

在日志中:

[通用] 创建未知类型的图像格式是错误的

记者[34067:1053969] 回应{ 代码 = 5; msg = "文件为空"; }

请帮帮我,怎么了?

信息.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];
}

【问题讨论】:

  • 从选择器视图中粉刷照片或上传照片不起作用??
  • @CodeChanger 没有从选择器视图中粉刷照片。
  • @МарияТимофеева 你检查过这个stackoverflow.com/questions/38236723/…
  • 您是否在实际获得此日志的位置调试代码?在 uploadPhoto 方法中?
  • @NiravD 是的,我检查了这个,但我们的问题并不相等。

标签: ios objective-c uipickerview uipickerviewcontroller uipickerviewdelegate


【解决方案1】:

根据您的代码和 cmets,我假设 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 的新对象并将其传递给您的 Upload 方法。

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-19
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 2023-03-06
    • 2017-07-10
    相关资源
    最近更新 更多