views:

41

answers:

1

Hi there, I've an app with a lot of masked image. For performance sake I ned to generate those masked image on disk and then incorporate them in the app (they'll be uploaded on demand nut not masked on the fly because it is already done).

I'm using this kind of coding

NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Test_%d.png",i]];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Test_%d.jpg",i]];

// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation([self maskImageWithStroke:image withMask:maskImage], 1.0) writeToFile:jpgPath atomically:YES];

// Write image to PNG
[UIImagePNGRepresentation([self maskImageWithStroke:image withMask:maskImage]) writeToFile:pngPath atomically:YES];

and it works perfectly for my intermediate images but not with the final one.

Here is the process of multiple mask and blan I use :

  1. take an image and mask it to obtain the maskedImage

  2. List item take the mask and resize it a bit bigger : biggerMask

  3. blend maskedImage and biggermask to have the maskedStrokedImage stroked by biggrMask (this is the only way I found to add an irregular stroke on the irregular masked image)

  4. mask maskedStrokedImage with biggerMask to obtain my final result.

The problem is : saving the image obtained on step 1 is ok : I've got a JPG and PNG with exactly what I need.

My goal is to save the result of step 4 to disk but the result is an imahe showing some part of the stroke and the rest is white ...

Any Idea why I'm unable to save the step 4 to disk ?

A: 

Solution found here https://devforums.apple.com/thread/67565?tstart=0

use mask with black shape on transparent background and use the following method. The masked inmage is saved perfectly to disk and can be imported as is to the screen it keeps its transparency ... grooovy !!!

-(UIImage*)maskImage:(UIImage *)givenImage withClippingMask:(UIImage *)maskImage { UIGraphicsBeginImageContext(givenImage.size); CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(currentContext, 0, givenImage.size.height); CGContextScaleCTM(currentContext, 1.0, -1.0); CGRect rectSize = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height); CGContextClipToMask(currentContext,rectSize, maskImage.CGImage); CGContextDrawImage(currentContext, rectSize, givenImage.CGImage); UIImage *imgMasked = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imgMasked; }

Tibi