ios - 在数组中添加捕获的图像并在 Collection View 中显示并上传到服务器 iOS
<p><p>我需要一张一张地捕获多张图像并将它们存储在一个数组中,在 Collection View 中显示存储的图像,然后将它们上传到服务器。</p>
<p>我对这个概念进行了很多搜索,但这些解决方案都不适合我,数组为空。</p>
<p>我试过下面的代码:</p>
<pre><code> - (IBAction)CaptureClicked:(id)sender {
UIImagePickerController *picker = [ init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (info == (NSString *) kUTTypeImage) {
;
UIImage *chosenImage = info;
self->ImageView.image = chosenImage;
;
NSData *imageData =;
;
;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先,您需要创建一个字典结构,将值设置在字典中,并以这种方式将此字典添加到数组中:</p>
<pre><code>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: )
</code></pre>
<p>这用于 <code>func imagePickerController(_picker: UIImagePickerController, didFinishPickingMediaWithInfo info: )</code></p>
<p>现在您可以根据 NSdata 在 Collection View 上显示图像。</p>
<p>现在在 web 服务中,我以这种方式传递图像数据:</p>
<pre><code>for i in 0 ..< arrImages.count {
let dictImage = arrImages as! NSDictionary
let fileData = dictImage as! NSData
let thumbData = dictImage as! NSData
let type = dictImage 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 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
</code></pre>
<p>您可以在您的网络服务中添加此代码片段,因为我向您展示了循环用于计算图像,在此基础上您可以发送多个图像。</p></p>
<p style="font-size: 20px;">关于ios - 在数组中添加捕获的图像并在 Collection View 中显示并上传到服务器 iOS,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43134902/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43134902/
</a>
</p>
页:
[1]