我需要一张一张地捕获多张图像并将它们存储在一个数组中,在 Collection View 中显示存储的图像,然后将它们上传到服务器。
我对这个概念进行了很多搜索,但这些解决方案都不适合我,数组为空。
我试过下面的代码:
- (IBAction)CaptureClickedid)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancelUIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerUIImagePickerController *)picker didFinishPickingMediaWithInfoNSDictionary *)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];
}
首先,您需要创建一个字典结构,将值设置在字典中,并以这种方式将此字典添加到数组中:
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 在 Collection View 上显示图像。
现在在 web 服务中,我以这种方式传递图像数据:
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
您可以在您的网络服务中添加此代码片段,因为我向您展示了循环用于计算图像,在此基础上您可以发送多个图像。
关于ios - 在数组中添加捕获的图像并在 Collection View 中显示并上传到服务器 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134902/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |