godotengine / godot-ios-plugins

MIT License
126 stars 48 forks source link

Is it possible to save image to photo album? #39

Open waci11 opened 1 year ago

waci11 commented 1 year ago

sorry , this is not a bug . but I really need to save image to photo album . it is a very important function for my app .

mingganglee commented 8 months ago

Env

Reference

Description

Here's the code I took a screenshot of and saved it to an album, hope it helps!

Note: The get_viewport().get_texture().get_data() code interferes with the main process, so it's a good idea to add a new thread to handle this part.

gdscript

var image: Image = get_viewport().get_texture().get_data()
image.flip_y()
var img_base64 = Marshalls.raw_to_base64(image.save_png_to_buffer())
screen_tools.screenshot(img_base64)
Toast.toast("screenshot success", 1, Toast.pos.top)

objective-c

void ScreenTools::screenshot(const String &img_base64) {
    NSString *base64String = [NSString stringWithUTF8String:img_base64.utf8().ptr()];
    NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    UIImage *image = [UIImage imageWithData:data];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    AudioServicesPlaySystemSound(1108);
}