2

I need to captured multiple images one by one and store them in an array, show stored images in a collection view and later upload them to server.

I searched a lot about this concept but none of those solutions worked for me, array is null.

I tried below code:

 - (IBAction)CaptureClicked:(id)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
 if (info[UIImagePickerControllerMediaType] == (NSString *) kUTTypeImage) {

    [self.videoController.view removeFromSuperview];
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self->ImageView.image = chosenImage;
    [picker dismissViewControllerAnimated:YES completion:NULL];

     NSData *imageData =[NSData dataWithData:UIImagePNGRepresentation(chosenImage)];

     [imageArray addObject:imageData];
     [_ImageCollectionVIew reloadData];
 }
halfer
  • 19,824
  • 17
  • 99
  • 186
Shikha Sharma
  • 451
  • 1
  • 5
  • 25

1 Answers1

2

First you need to make a structure of dictionary set the values in a dictionary and add this dict in an array in this way:

let imageData = UIImageJPEGRepresentation(image, 0.0)!
 dict.setObject(imageData, forKey: MediaFilesMetaDataConstants.FileData as NSCopying)
  dict.setObject(IMAGENAME, forKey: MediaFilesMetaDataConstants.FileName as NSCopying)
  dict.setObject(MediaFilesMetaDataConstants.FileType_Image, forKey: MediaFilesMetaDataConstants.FileType as NSCopying)
  dict.setObject(thumbData, forKey: MediaFilesMetaDataConstants.Thumbnail as NSCopying)


kAppDelegate.arrMediaFiles.addObjects(from: [dict as Any])

This is used in func imagePickerController( _ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

Now you can show image on collection view on the basis of NSdata.

Now in webservice I am passing the image data in this way:

for i in 0 ..< arrImages.count {
            let dictImage = arrImages[i] as! NSDictionary
            let fileData = dictImage[MediaFilesMetaDataConstants.FileData] as! NSData
            let thumbData = dictImage[MediaFilesMetaDataConstants.Thumbnail] as! NSData
            let type = dictImage[MediaFilesMetaDataConstants.FileType] as! String
            if(type == MediaFilesMetaDataConstants.FileType_Image) {
                mimetype = "application/octet-stream"
            } else {
                mimetype = "video/mov"
            }

if(i == 0) {
                if(fileData.length > 0) {
                    body.appendString(string: "--\(boundary)\r\n")
                    body.appendString(string: "Content-Disposition: form-data; name=\"file_name0\"; filename=\"\(dictImage[MediaFilesMetaDataConstants.FileName] as! String)\"\r\n")
                    body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
                    body.append(fileData as Data)
                    body.appendString(string: "\r\n")
                }

                    if(thumbData.length > 0) {
                        body.appendString(string: "--\(boundary)\r\n")
                        body.appendString(string: "Content-Disposition: form-data; name=\"thumbnail0\"; filename=\"thumbImage.png\"\r\n")
                        body.appendString(string: "Content-Type: application/octet-stream\r\n\r\n")
                        body.append(thumbData as Data)
                        body.appendString(string: "\r\n")
                    }



            }

request.httpBody = body as Data

You can add this code snippet in your webservice, as I show you that loop is used for calculating images, on that basis you can sent multiples images.

Gourav Joshi
  • 2,419
  • 2
  • 27
  • 45