Save image

I have an image in an Imageview. Saving the image to the iPhone crashes the app with a ‘unsupported image format’ error.

Dim saveFile As FolderItem = SpecialFolder.Documents.Child("public.jpeg") ImageView1.Image.WriteToFile(saveFile,"test.jpeg")

Tried ‘jpeg’ and ‘jpg’ and also a PNG image.
Also, I can’t seem to find the Photo Album as a Special Folder.

[quote=155088:@Vincent Verweij]I have an image in an Imageview. Saving the image to the iPhone crashes the app with a ‘unsupported image format’ error.

Dim saveFile As FolderItem = SpecialFolder.Documents.Child("public.jpeg") ImageView1.Image.WriteToFile(saveFile,"test.jpeg")

Tried ‘jpeg’ and ‘jpg’ and also a PNG image.
Also, I can’t seem to find the Photo Album as a Special Folder.[/quote]

Look at http://xojo.helpdocsonline.com/iosimage$WriteToFile

You are confusing the name of the file and the type to save. This works :

Dim saveFile As FolderItem = SpecialFolder.Documents.Child("mypic.jpeg") // Name of the file ImageView1.Image.WriteToFile(saveFile,"public.jpeg") // public.jpeg is the type of the file

Thanks Michel, sometimes the solution is so simple.
Is there a way to save to the Camera Roll (Photo Album) instead of the Documents Folder?

[quote=155098:@Vincent Verweij]Thanks Michel, sometimes the solution is so simple.
Is there a way to save to the Camera Roll (Photo Album) instead of the Documents Folder?[/quote]

Not natively, but a declare is probably possible :
http://stackoverflow.com/questions/14344531/ios-save-ciimage-to-camera-roll

It would be nice to be able to add includes to a project so they get linked upon build. That should be possible. I wonder what Xojo engineers think about that ?

Just saw this conversation - its entirely possible with declares. Just pop this short function into a module and call it like “img.SaveToCameraRoll”

Sub SaveToCameraRoll(extends img as iOSImage) declare sub UIImageWriteToSavedPhotosAlbum lib "UIKit" (img as ptr, target as ptr, sel as ptr, info as ptr) UIImageWriteToSavedPhotosAlbum(img.Handle,nil,nil,nil) End Sub

[quote=156706:@Jason King]Just saw this conversation - its entirely possible with declares. Just pop this short function into a module and call it like “img.SaveToCameraRoll”

Sub SaveToCameraRoll(extends img as iOSImage) declare sub UIImageWriteToSavedPhotosAlbum lib "UIKit" (img as ptr, target as ptr, sel as ptr, info as ptr) UIImageWriteToSavedPhotosAlbum(img.Handle,nil,nil,nil) End Sub [/quote]

Thank you Jason.

I just added that method to XojoiOSWrapper
https://github.com/Mitchboo/XojoiOSWrapper

You’re welcome

sir i try to save image from the simulator but it didn’t work

sir can you give a sample program like you explain it.

iOSKit is an example project. The function is in the Extensions module: https://github.com/kingj5/iOSKit

thanks for sharing, i tried on the iOS device to export the data as JPEG on the network… and i still receiving it as a immutable ios format

Dim bitmap as new iOSBitmap(ImageView1.Image.Width,ImageView1.Image.Height,ImageView1.Image.Scale)
bitmap.Graphics.DrawImage ImageView1.Image,0,0

// Build our ContentRequest
dim mData as MemoryBlock = bitmap.Image.ToData(“public.jpeg”)

the the image is still not convertible to JPG :-/

any idea ?

thanks :slight_smile:

See here for converting to jpeg:
https://forum.xojo.com/20774-writetofile-fromfile-causes-a-iosimage-to-be-rotated/p2#p400515

This item does not exist
Declare function UIImageJPEGRepresentation lib UIKitLib (obj_id as ptr,compressionQuality As CGFloat) as ptr

hum… apple removed it ?

That is a constant from my iOSKit. Also needed for the NSData methods there. Download at: github.com/kingj5/iOSKit