2

我需要一张一张地捕获多张图像并将它们存储在一个数组中,在集合视图中显示存储的图像,然后将它们上传到服务器。

我对这个概念进行了很多搜索,但这些解决方案都不适合我,数组为空。

我尝试了以下代码:

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

1 回答 1

2

首先,您需要使字典结构设置字典中的值并以这种方式将此字典添加到数组中:

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])

这用于func imagePickerController( _ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

现在您可以在 NSdata 的基础上在集合视图上显示图像。

现在在网络服务中,我以这种方式传递图像数据:

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

您可以在您的网络服务中添加此代码片段,因为我向您展示了循环用于计算图像,在此基础上您可以发送多个图像。

于 2017-03-31T07:44:18.150 回答