I followed @Rob answer and its drawn as I want...but when I saved this image....stroke not transparent anymore
Objective-C How to avoid drawing on same color image having stroke color of UIBezierPath
For save image I written this code
-(void)saveImage {
UIGraphicsBeginImageContextWithOptions(self.upperImageView.bounds.size, NO, 0);
if ([self.upperImageView respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
[self.upperImageView drawViewHierarchyInRect:self.upperImageView.bounds afterScreenUpdates:YES]; // iOS7+
else
[self.upperImageView.layer renderInContext:UIGraphicsGetCurrentContext()]; // pre iOS7
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndPDFContext();
UIImageWriteToSavedPhotosAlbum(self.upperImageView.image, self,@selector(image:didFinishSavingWithError:contextInfo:), nil);
}
You are either setting the
alpha
of the image view with the paths to 1.0 somewhere, or you are using something that doesn't permit transparencies (e.g.UIGraphicsBeginImageContextWithOptions
withYES
for the opaque parameter, or staging the image in a format that doesn't preserve alpha, such as JPEG).A few additional thoughts:
I notice that you're only drawing
upperImageView
. If you want the composite image, you need to draw both. Or are you only trying to save one of the image views?(For those unfamiliar with that other question, the entire point was how to draw multiple paths over an image, and have the full set of those paths with the same reduced alpha, rather than having the intersection of paths lead to some additive behavior. This was accomplished by having two separate image views, one for the underlying image, and one for the drawn paths. The key to the answer to that question was that one should draw the paths at 100% alpha, but to add that as a layer to a view that, itself, has a reduced alpha.)
What is the
alpha
of the image view upon which you are drawing.NB: In the answer to that other question, when saving a temporary copy of the combined paths. we had to temporarily set the alpha to 1.0. But when saving the final result here, we want to keep the "path" image view's alpha at its reduced value.
Unrelated, but you faithfully transcribed a typo (since fixed) in my original example where I accidentally called
UIGraphicsEndPDFContext
rather thanUIGraphicsEndImageContext
. You should use the latter.So, considering two image views, one with the photograph and one with the drawn path, called
imageImageView
(withalpha
of 1.0) andpathsImageView
(withalpha
of 0.5), respectively, I can save the snapshot like so:When I did that, the resulting composite image was in my photo album: