Masking an image in Swift using CALayer and UIImage

Masking an image in Swift using CALayer and UIImage

By : user2951282
Date : November 18 2020, 01:01 AM
I think the issue was by ths following , Unfortunately you've asked your question rather badly - you have not said what it is that you are actually trying to do! It looks, however, as if you might be trying to punch a rectangular hole in your image view using a mask. If so, your code has at least three huge flaws.
code :
 var color = UIColor(white: 1.0, alpha: 1.0)
// ... and then, later ...
color = UIColor(white: 1.0, alpha: 0.0)
var maskLayer = CALayer()
maskLayer.frame = CGRectMake(
    0, 0, self.imageView.bounds.width, self.imageView.bounds.height)
maskLayer.contents = maskImage.CGImage

Share : facebook icon twitter icon
Drawing a UIImage into CALayer.contents does not render image data

Drawing a UIImage into CALayer.contents does not render image data

By : BerislavSenad
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am having a wicked time trying to get an image to show up in a CALayer.contents. It seems rather straight forward but I cannot get the image to render no matter what I do. The CALayer renders fine as I can see its background color and corner radius, but the image will not load. , Remove this line:
code :
[self.imageLayer setNeedsDisplay];
Change color of an UIImage with masking and replacing that image with a colored image

Change color of an UIImage with masking and replacing that image with a colored image

By : TnL
Date : March 29 2020, 07:55 AM
Does that help I'm not sure that grabbing an imageRef using a CGBitmapContextCreateImage() call is appropriate inside a UIGraphics image context. Every example I've ever seen uses
code :
  UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
Image masking & create new UIImage

Image masking & create new UIImage

By : David Wajnryt
Date : March 29 2020, 07:55 AM
around this issue Here is the issue i am facing:
code :
- (UIImage *)croppedPhoto {
    // For dealing with Retina displays as well as non-Retina, we need to check
   // the scale factor, if it is available. Note that we use the size of teh cropping Rect
    // passed in, and not the size of the view we are taking a screenshot of.
     CGRect croppingRect = CGRectMake(imgMaskImage.frame.origin.x,
    imgMaskImage.frame.origin.y, imgMaskImage.frame.size.width,


    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES,
    [UIScreen mainScreen].scale);
    } else {

    // Create a graphics context and translate it the view we want to crop so
    // that even in grabbing (0,0), that origin point now represents the actual
    // cropping origin desired:
         CGContextRef ctx = UIGraphicsGetCurrentContext();
         CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y);
    [self.view.layer renderInContext:ctx];

   // Retrieve a UIImage from the current image context:
   UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();

    // Return the image in a UIImageView:
   return snapshotImage;
Swift UIImage from CALayer crash

Swift UIImage from CALayer crash

By : user4973581
Date : March 29 2020, 07:55 AM
it should still fix some issue Try changing layer.frame.size throughout to layer.bounds.size. Layers don't really have frames, except as a slippery derived notion, so you are ending up with a zero sized graphics context, which is invalid.
Display an image or UIImage with a plain CALayer

Display an image or UIImage with a plain CALayer

By : user3809528
Date : March 29 2020, 07:55 AM
Any of those help I've often read that using a CALayer rather than a UIImageView is an performance boost when it comes to heavy image usage. That makes sense, because UIImageView causes 3 copies of the image in memory, which is needed for Core Animation. But in my case I don't use Core Animation.
Related Posts Related Posts :
  • consecutive statement need to have ; error in swift
  • Trying to create a 32 bpc NSBitmapImageRep, getting hit with errors
  • Does locationManager:didUpdateLocations: method called at the same time in extension and its containing app
  • Can't get Data using SwiftyJSON
  • Convert String to UnsafeMutablePointer<char_t> in Swift
  • inout param with empty object since nil won't work
  • How to align a mask onto an image
  • compiler forcing self inside static method of a function while using dispatch_sync
  • private static variable in struct
  • Conditional "in" for arrays in Swift
  • Extension for Double to accept String as initializer in Swift
  • Modifying UIButton Programmatically
  • Pass type to generic function and compare
  • How to customise the look of the PFSignUpViewController
  • How to take screen shot programmatically (Swift, SpriteKit)
  • Setting NavigationController's NavigationBar translucent property to false causes extra padding
  • Create multiple UIViews when the user taps the screen
  • Filter Array of [AnyObject] in Swift
  • How to add ParseUI to a swift project
  • Can Swift enums have multiple raw values?
  • Using a function() variable in a different Class - Swift
  • KVC without NSObject (NSPredicate)
  • Any? to Dictionary Cast and retrieving value
  • NSCharacterSet.characterIsMember() with Swift's Character type
  • SCNScene: Calculate projected size of an object
  • How to use contains on a swiftyJSON object
  • Index of element in sorted()
  • How do I initialise a new NSDocument instance in Swift?
  • Multiple functions with the same name
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org