How to save fbo to Photo Album in IOS?

#1

I know we can use ofxiOSGetDocumentsDirectory() to save the fbo in the Documents directory.

 void ofApp::touchDoubleTap(ofTouchEventArgs & touch){
     ofPixels pixels;
     fbo.readToPixels(pixels);
     ofSaveImage(pixels, ofxiOSGetDocumentsDirectory() +ofGetTimestampString() + "_image.png");
 }

We can find it in “Documents”,But how to save fbo to the PhotoAlbum?Somebody know how to solve this problem?

#2

@Wenzy90, try this:

#3

Thansks@mantissa 。The link said may we should use " UISaveVideoAtPathToSavedPhotosAlbum"。Later, i found another similar solution in here.(Saving ofImage in Photo Library)

It can save an image in Photo Album.But something is wrong.

void ofApp::setup(){	
fbo.allocate(3000,3000,GL_RGBA);
fbo.begin();
ofBackground(255,0,0);
ofSetColor(0);
ofDrawCircle(1500,1500,500);
fbo.end();
chanNum = 1;

}

void ofApp::touchDoubleTap(ofTouchEventArgs & touch){

ofPixels pixels;
fbo.readToPixels(pixels);

ofImage photo;
photo.setFromPixels(pixels);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, photo.getPixels(), (photo.getWidth()*photo.getHeight() * 3), NULL);
CGImageRef imageRef = CGImageCreate(photo.getWidth(), photo.getHeight(), 8, 24, 3*photo.getWidth(), CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]);
UIImage *output = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(output,nil,nil,nil);

}

It seems some pixels are separate?

#4

Hey there, my guess is that you actually want to save the alpha channel as well. looks like you’re just doing the RGB. I would try something like:

ofPixels pixels;
pixels.allocate(ofGetWidth(), ofGetHeight(), 4);
fbo.readToPixels(pixels);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels.getData(), (pixels.getWidth()*pixels.getHeight() * 4), NULL);
CGImageRef imageRef = CGImageCreate(pixels.getWidth(), pixels.getHeight(), 8, 32, 4*pixels.getWidth(), CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]);
UIImage *output = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(output,nil,nil,nil);

However, I just tried this and ended up with just a black image, so maybe something else is also amiss…

1 Like
#5

Hi ,did you figurated out?
I’m trying it.

#6

try in it but can even get that code i got an error i this this line:

#7

did anyone ever get saving to photos to work?

#8

No luck yet!

#9

@aaa i found this addon https://gist.github.com/robotconscience/4718ae1ea6828278d9bf
Is saving in the photo album but i’m getting a black image tho.
Use it like a reguar addon
h
ofxiOSImageSaver pic;

mm

photo.setFromPixels(backData);
pic.saveImage(photo);
#10

Any progress/success on this ?

#11

Hi @Dorald,
For our project we needed to save video created from oF to Photo Album, I used AVAssetWriter to first create a video and then used PHPhotoLibrary to save the video to the Photo Album. Maybe this works also for images.

1 Like
#13

You need to save the FBO into a ofImage and then into a UIimage which can then be saved to Photos

ofFbo screen;
ofImage photo;
screen.allocate(canvas.bounds.width,canvas.bounds.height,GL_RGBA);
screen.begin();
ofClear(0,0,0,255);
ofSetColor(255,255,255,255);
//DRAW YOUR FBO HERE
screen.end();
ofPixels pix;
screen.readToPixels(pix);
photo.setFromPixels(pix);
        

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, photo.getPixels().getData(), (photo.getWidth()*photo.getHeight() * 4), NULL);
CGImageRef imageRef = CGImageCreate(photo.getWidth(), photo.getHeight(), 8, 32, 4*photo.getWidth(), CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]);
UIImage *output = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(output,nil,nil,nil);

I’ve pulled this from a project I’ve worked on and it works, what you might need to do is save the “photo” image to a directory and then read it back from the directory before the CGData etc point.

Also make sure that you have setup the privacy permissions in the Build Settings

1 Like