libimobiledevice-win32 / imobiledevice-net

.NET (C#, VB.NET,...) bindings for libimobiledevice
GNU Lesser General Public License v2.1
297 stars 77 forks source link

Struggling to upload an Image to my iOS mobile device #166

Closed iDanielD closed 3 years ago

iDanielD commented 3 years ago

Hey,

I am having trouble uploading pictures from my computer to my iOS mobile device. So far I have tried to use the method "IMobileImageMounterApi.mobile_image_mounter_upload_image", but I have no idea what to use as a parameter. Below I have my code. Could someone help me with this?

private void UploadImage(Image image)
{
            PlistHandle plistHandle = null;

            var imageMounter = LibiMobileDevice.Instance.MobileImageMounter;
            var byteArray = ImageToByteArray(image); // Returns a byte array from the selected image from my Computer.

            imageMounter.mobile_image_mounter_start_service(DeviceHandle, out var mobileImageMounterClientHandle, null);
            imageMounter.mobile_image_mounter_upload_image(mobileImageMounterClientHandle, "jpg", (uint)byteArray.Length, 
            null, 0, null, ??);
}

Thank you in advance!

qmfrederik commented 3 years ago

@iDanielD mobile_image_mounter is used to upload developer disk images (i.e. file archives), not pictures.

You can probably use AFC (the Apple File Conduit) to transfer files to/from your phone. Haven't done it myself, though.

iDanielD commented 3 years ago

@qmfrederik Thank you for your quick response. I just tried using the AFC, but I cant see there an option to upload an image. I have updated my Code to something like this:

private void UploadImage(Image image)
        {
            var afcApi = LibiMobileDevice.Instance.Afc;

            var byteArray = ImageToByteArray(image); // Returns a byte array from the selected image from my Computer.

            afcApi.afc_client_start_service(DeviceHandle, out var afcClientHandle, null).ThrowOnError();

            afcApi.??
        }

I linked all possbile afc methods below. image image image

qmfrederik commented 3 years ago

@iDanielD AFC allows you to open a file (afc_file_open) and then write to that file (afc_file_write). That's how you would upload a file. It's very similar to the file APIs in C (fopen, fwrite, fread,...)

You'd need to figure out in which directory you need to save the images.

iDanielD commented 3 years ago

@qmfrederik Alright I will test it myself and write back if I got any results. Thank you.

iDanielD commented 3 years ago

@qmfrederik I cannot get it to run. I don't understand how I should upload such a picture, if I open an already existing file from my cell phone and then write new information into it? Will the picture not be overwritten then?

This is what I have tried (Not working)

private void UploadImage(Image image)
        {
            var afcApi = LibiMobileDevice.Instance.Afc;
            ulong handle = 0;
            uint bytesWritten = 0;

            var byteArray = ImageToByteArray(image); // Returns a byte array from the selected image from my Computer.

            afcApi.afc_client_start_service(DeviceHandle, out var afcClientHandle, null).ThrowOnError();

            afcApi.afc_read_directory(afcClientHandle, @"/DCIM/101APPLE", out var dir).ThrowOnError();
            afcApi.afc_file_open(afcClientHandle, @"/DCIM/101APPLE/IMG_1362", AfcFileMode.FopenRdappend, ref handle).ThrowOnError();
            afcApi.afc_file_write(afcClientHandle, handle, byteArray, (uint)byteArray.Length, ref bytesWritten).ThrowOnError();
        }
qmfrederik commented 3 years ago

If you specify a path to a file which does not exit, afc_file_open should create a new file on the device.

iDanielD commented 3 years ago

Well, I see the problem. The file is being written, but I can't see it on my phone. I thought that if I write "@"/DCIM/101APPLE "" in the path, it will be displayed in this folder so I can view it in windows. But this is not the case. Is there maybe a common path for the albums on my iPhone where I can store my file and see it?

qmfrederik commented 3 years ago

You should be able to use afc_read_directory to get a listing of the directory contents, and get an idea of the directory structure (and know to which folder you can upload your picture).

Also make sure to properly close the file handle with afc_file_close, etc... .

iDanielD commented 3 years ago

@qmfrederik Ok after several attempts to find the right path, I decided to create a new album and save the pictures in it. Do you perhaps know a way to create a new album on the device where I could save my pictures?

iDanielD commented 3 years ago

Has anyone found a solution?

iDanielD commented 3 years ago

So the solution would be to edit the "PhotoData/Photos.sqlite" database. This way the added or deleted Images would als be added or removed from the gallery.