这是我的情景:
这就是我所做的:
将图像保存到Parse后端:
promise = promise.then(function(){
/* Download image */
return Parse.Cloud.httpRequest({
url: imageUrl
});
}).then(function(imageResponse){
/* Use parse-image cloud module to get image */
var image = new Image();
return image.setData(imageResponse.buffer);
}).then(function(image){
/* Save image as parse file */
var imageInBase64 = image.data().base64;
var parseFile = new Parse.File(imageName, {base64: imageInBase64});
return parseFile.save();
}).then(function(parseFile){
/* Set PFFile to an object */
newEvent.set("eventImage", parseFile);
});
然后我通过点击相关对象的PFFile字段验证了PFFile已保存在后端。当我点击文件时,我看不到图像,而是看到以下文字。
{"_ApplicationId":"xxxx","_JavaScriptKey":"xxxx","_ClientVersion":"js1.6.14","_InstallationId":"xxxx","_SessionToken":"r:xxxx"}
在PFImageView中显示相同的图像:
@IBOutlet weak var eventImage: PFImageView!
if let imageFile = selectedEvent.eventImage as PFFile {
eventImage.image = UIImage(named: "Event Image")
eventImage.file = imageFile
eventImage.loadInBackground()
}
不幸的是,这不起作用,PFImageView只是空白。看起来PFFile是基于我得到imageFile.url和imageFile.name的以下结果而下载的:
imageFile.url:https://files.parsetfss.com/xxxx/tfss-xxxx imageFile.name:tfss-xxxx
我想知道这里的问题是如何在云代码中将图像上传到Parse。在这里,我将其转换为base64,然后保存文件。
答案 0 :(得分:0)
解决:
问题在于使用var imageInBase64 = image.data().base64;
。我的替代云代码如下:
promise = promise.then(function(){
/* Download image */
return Parse.Cloud.httpRequest({
url: imageUrl
});
}).then(function(imageResponse){
/* Use parse-image cloud module to get image */
var image = new Image();
return image.setData(imageResponse.buffer);
}).then(function(image){
format = image.format();
return image.data();
}).then(function(buffer)){
/* Save image as parse file */
var imageInBase64 = buffer.toString("base64");
var parseFile = new Parse.File(imageName + "." + format, {base64: imageInBase64});
return parseFile.save();
}).then(function(parseFile){
/* Set PFFile to an object */
newEvent.set("eventImage", parseFile);
});