Saving ofImage in Photo Library

Hi !
I need it in my project and I did not find how to do it in the forum.
Or happend that a code works fine in for example 5.0, 5.1, but crash in 6.x.
I test some stuff and finnaly works, so here I share how to do it: (sorry if is in some addon o things like that, ofxImagePicker has an “save” method but says it didnt work weel).

  
ofImage photo;  
photo.grabScreen(0, 0, ofGetScreenWidth(), ofGetScreenHeight());  
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);  

greets, I hope it serves

Thanks xxxlol!! I tryed to do the same with my code, but nothing worked. My code was this:

void ofxiOSImagePicker::saveImageFromPixel(int _size, unsigned char pixels[])
{
  NSData* data = [NSData dataWithBytes:(const void *)pixels length:sizeof(unsigned char)*_size];
  UIImage* img = [[UIImage alloc] initWithData:(NSData *)data];
  UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
	printf("saved from pixel");
}

Now i used your code inside my method and now it works! Thanks a lot!

void ofxiOSImagePicker::saveImageFromPixel(int width, int height, int pixelForChannel, unsigned char pixels[])
{
  CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, width*height*pixelForChannel, NULL);
  CGImageRef imageRef = CGImageCreate(width, height, 8, 24, pixelForChannel*width, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);
  NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]);
  UIImage *output = [UIImage imageWithData:imageData];
  UIImageWriteToSavedPhotosAlbum(output,nil,nil,nil);
}

Anyway, do you know why my code doesn’t work? Any idea?