I am trying to make some UIImages into one single image that I can save in my photo album. But does layer.renderInContext seem to not account for the account?
Current behavior: the photo saves, and I see mosaicLayer, without the maskLayer masking effect.
Expected behavior: the photo is saved, and I see the image in my view, and mosaicLayer is masked on top of it.
I use the following code to mask an image
UIImage *maskImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mask" ofType:@"png"]]; maskLayer = [[UIImageView alloc] initWithImage:maskImg]; maskLayer.multipleTouchEnabled = YES; maskLayer.userInteractionEnabled = YES; UIImageView *mosaicLayer = [[UIImageView alloc] initWithImage:img]; mosaicLayer.contentMode = UIViewContentModeScaleAspectFill; mosaicLayer.frame = [imageView bounds]; mosaicLayer.layer.mask = maskLayer.layer; [imageView addSubview:mosaicLayer];
And then I use this code to save my folded image:
UIGraphicsBeginImageContext(imageView.bounds.size); [imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *saver = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(saver, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
EDIT . Applies the mask correctly.
-(IBAction) saveImage { UIImage * saver = nil; CGImageRef image = imageView.image.CGImage; size_t cWidth = CGImageGetWidth(image); size_t cHeight = CGImageGetHeight(image); size_t bitsPerComponent = 8; size_t bytesPerRow = 4 * cWidth;
ios objective-c iphone uiimage mask
ThomasM
source share