Upload ios image with qt
-
Hi , i use qt 5.3 on mac. I 've accessed to ios gallery with UIImagePickerController and i've get selected image url like this:
assets-library://asset/asset.jpg?id=..&ext=jpg
Now i want to upload this image on server. I converted the url to QString and called myUploadFunction(QString path). This function gets a path(that is a real path of image on device), creates a QFile, reads data from the file and posts data:
@
QFile *file = new QFile(path);
file->open(OIDevice::ReadOnly);
QByteArray fileData = file->readAll();
...
networkAccessManager->post(...)
@I know the upload method works on android and windows desktop. But in ios,i think i can not create a file from the image url (assets-library://asset/asset.jpg?id=..&ext=jpg). I need image actual path in ios. how can i get an image real path from it's url on ios to upload it?
Thanks in advance -
I found a way to get an image file real path on ios from "here":https://github.com/questbeat/QBImagePickerController/issues/6
path on simulator:
file:///Users/mrsranjkesh/Library/Application Simulator/iPhone Simulator/7.0.3/Applications/9CB32.../Document/Koala.jpg
When i give this path to my upload file, I get this error: QIODevice::read Device not open. When i tested file.exist(), i got "false"
please reply. Thanks -
Did you manage to solve your problem.
I'm facing the same problem. When I use FileDialog, fileUrl looks like "file:assets-library:/asset/asset.JPG%3Fid=78FD7629-DBEE-4279-ACAE-A54F1FEB9B58&ext=JPG" and I cannot assign this as an image source.
-
@ahmad88me Did you tried to use uri like "assets-library:/asset/asset.JPG%3Fid=78FD7629-DBEE-4279-ACAE-A54F1FEB9B58&ext=JPG" ?
-
Hello i've got the same problem :(
Did you resolve it ? -
Hello,
Same bug for me.There is no need to have a beautifull picture picker if we can't upload one of those pictures.
One thing I don't understand :
QML Image is able to understand a path like "file:assets-library:/asset/asset.JPG..."
But neither QFile, nor QFileInfo, nor QImage can do that.
Do you have an idea why ? -
Guys, I found a solution :
QString path("file:assets-library:/asset/asset.JPG..."); QUrl url(path); path = url.toLocalFile();
-
@Vi67 said:
QString path("file:assets-library:/asset/asset.JPG...");
QUrl url(path);
path = url.toLocalFile();Thanks man, you saved me!
-
@renatobibiano I tried:
QString path("file:assets-library:/asset/"+fileInfo.fileName()); QUrl url(path); path = url.toLocalFile();
path then is
"file:assets-library:/asset/asset.PNG?id=FB873B83-78E1-4B18-8601-0CD2CBD92308&ext=PNG"
but url.toLocalFile() gives
"assets-library:/asset/asset.PNG"
so this doesnt work for me. any idea ?
-
ekkescorner Qt Champions 2016replied to ekkescorner on 25 Oct 2017, 22:09 last edited by ekkescorner
found out HowTo get access to the Images on iOS.
last entry of QStandardPath::PicturesLocation gives me access to
assets-library://
QDir entryInfoList() gives me QFileInfo and QFileInfo provides the filePath :
"assets-library://asset/asset.PNG?id=E8CE839A-29EA-42B2-A8FD-89B57ABEC765&ext=PNG"
QFileInfo() gets access to the Photos so of course also QFile can read them and so I can upload to REST service.
was confused by the unusual pathes of Photos stored on iPhone.One missing point: I also want to use the Photos as source of QML Image.
Normaly this is working to display an Image using filePath from QFileInfo:Image { id: theImage anchors.fill: parent source: Qt.resolvedUrl("file://"+currentFile.path) fillMode: Image.PreserveAspectFit }
but for files from iOS assets-library I’m getting
QML Image: Cannot open: file://assets-library//asset/asset.PNG
any idea HowTo provide a correct URL to Image ?
-
Hi ekke,
Have you considered writing a very lightweight C++ class for an image provider? -
@kshegunov said in Upload ios image with qt:
Have you considered writing a very lightweight C++ class for an image provider?
have thought about this
but was hoping that there's a way to construct an URL from QML
seems it isn't -
@ekkescorner said in Upload ios image with qt:
but was hoping that there's a way to construct an URL from QML
seems it isn'tI think Thiago's objection goes more in the direction of security than anything else, as unless you're going to provide a full fledged parser that sanitizes the input (removing the special characters and so on) it's usually a bad idea to construct paths by parts manually. And this way you'd also wouldn't be able to benefit from any (security or other) patches that go into Qt's path handling.
If it were me I'd implement a basic provider on the C++ side, as it seems as the cleaner approach. -
@kshegunov finally I figured it out HowTo deal with iOS Images (Photos) without the need of creating an ImageProvider.
The file pathes are slightly different if you compare the filePath you got from C++ (QFileInfo) and the filePath you got from native iOS ImagePicker via QML FileDialog.
Getting the iOS Photos from C++
QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).last() points to "assets-library://"
QDir gives you a list of QFileInfo
Then it's easy to get the FilePath from C++ QFileInfo
will be something like this:"assets-library://asset/asset.PNG?id=C6E6...77E9&ext=PNG"
Coming the other way from QML use a special configured FileDialog:
import QtQuick 2.9 import QtQuick.Dialogs 1.2 FileDialog { folder: shortcuts.pictures }
This FileDialog opens a native iOS Photo Picker
Select a File and get the FilePath:theImage.source = myDialog.fileUrl
FileUrl will be per ex:
"file:assets-library://asset/asset.PNG%3Fid=C6E6...77E9&ext=PNG"
The QML path is needed to display the iOS asset as QML Image source,
the C++ path is needed to open the iOS asset from QFileConversion from QML to C++ Path:
QUrl url(path); path = url.toLocalFile();
Conversion from C++ to QML Path:
path = "file:"+fileInfo.filePath().replace("?","%3F")
-
@ekkescorner said in Upload ios image with qt:
Thanks for sharing the solution, ekke. There's something I find curious here, though:"file:assets-library://asset/asset.PNG%3Fid=C6E6...77E9&ext=PNG"
It's really surprising that only the
?
is encoded in this case. I'd have expected all the special characters to be encoded according to URL rules. It's just odd. In any case for conversion from C++ to QML Path:path = "file:"+fileInfo.filePath().replace("?","%3F");
I'd research the possibility to use:
path = "file:"+ QUrl::fromLocalFile(fileInfo.filePath()).toString();
or something of that nature. Especially if it's possible the local file is to be named in non-latin letters.
-
@kshegunov yep - was surprising to me, too that only '?' is encoded. didn't found another way to manage this.
but for this special use-case there's no risk to get in trouble with non-latin letters because the filenames are pseudonames generated by iOS based on a UUID iOS generated -
@ekkescorner how to access ios system photo album;
can you help me?