0

I have a UIView with different CALayers with different size.

When I am trying to save UIView as image to gallery it looses its transparency:

My code is:

- (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

//Getting image

 UIImage *img=[self imageWithView:MainView];
 UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);

Note:When I debug the code, it displays transparent image, but when I see in gallery it displays with white Background

Indrajeet
  • 5,490
  • 2
  • 28
  • 43
Urja Sheth
  • 41
  • 6
  • how u set the transparency? – Indrajeet Jul 16 '15 at 12:30
  • I have a UIView,which have different CALayers,each layer's size is different.for example, layer's size is smaller then the MainView's size – Urja Sheth Jul 16 '15 at 12:32
  • 1
    NO problem, it will show either black background or white background but actually it is transaparent – Kumar Jul 16 '15 at 12:32
  • what can I do to remove this black or white background? – Urja Sheth Jul 16 '15 at 12:35
  • Btw instead of `[UIScreen mainScreen].scale` you may pass `0.f`, as it's the same and a bit more readable :) Ofc your way is also proper. Also in my opinion more suitable name would be `imageFromView`. For code sanity it's good to have same styling everywhere - eg once you write spaces before and after `=` and second later you don't type them. It will be more readable by other devs later. – Nat Jul 16 '15 at 12:41
  • Are you sure iOS image gallery shows that image is transparent? I think it's usually either white or black bg, so your code is probably ok. – Nat Jul 16 '15 at 12:46
  • Thanks for your suggestion @Vive, but getting same output after passing 0.f scale. – Urja Sheth Jul 16 '15 at 12:46
  • @UrjaSheth That was just code quality tip, not the solution (as I wrote "it's the same but more readable"). Solution is I think your code is ok, it's iOS image gallery what is showing alpha as white/black. – Nat Jul 16 '15 at 12:46
  • Yes,I am sure.image saved with white background. – Urja Sheth Jul 16 '15 at 12:47
  • @UrjaSheth did you try this: http://stackoverflow.com/a/10279075/849616? – Nat Jul 16 '15 at 12:49
  • @Vive I tried this but did not work. – Urja Sheth Jul 16 '15 at 12:57

1 Answers1

0

I've tried your code. You can try following implementation:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *tmpView = [UIView new];
    tmpView.frame = self.view.bounds;
    tmpView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tmpView];

    UIImage *img = [self imageWithView:tmpView];
    [self saveInJPGFormat:img];
    [self saveInPNGFormat:img];
}

- (void)saveInJPGFormat:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

- (void)saveInPNGFormat:(UIImage *)image {
    NSData* imageData =  UIImagePNGRepresentation(image);
    UIImage* pngImage = [UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil);
}

- (UIImage *) imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

Your method saves as JPG to camera roll. JPGs aren't capable to keep alpha channel. Second method taken from https://stackoverflow.com/a/10279075/849616 saves image as PNG. I can open it and I can see through the image (alpha channel is saved).

BTW: ofc that's very dirty and quick code. In reality you should do a category on UIImage for these methods. Also keep MVC and either views&layers stick to storyboards or to separate UIView subclass.

Images from my photo gallery: CameraRoll Screen of preview so you're sure it's empty: enter image description here

So the method is working.

Community
  • 1
  • 1
Nat
  • 12,032
  • 9
  • 56
  • 103