Home > Mobile >  Ios do irregular images of hollow out (hollow)
Ios do irregular images of hollow out (hollow)

Time:09-17

Problem: need to effect the characters in the picture images around a custom frame, such as foot, plum flower pattern, and so on, border shows character images after middle hollow out .
Check network solution is only one "CAShapeLayer and UIBezierPath", but I can't do these solutions specified irregular pattern. A great god, please how to solve?
Border patterns:

The effect of image:

CodePudding user response:

Solution: use the mask can.
//
//ViewController. M
//TestBKColor
//
//Created by frank. Zhai on 2020/4/2.
//Copyright? 2020 frank. Zhai. All rights reserved.
//

# import "ViewController. H"
# import "MyView. H"
@ interface ViewController () {

}
@ end

@ implementation ViewController
UIImageView * imageV;
- (void) viewDidLoad {
[super viewDidLoad];
UIImage * imgSource=[UIImage imageNamed: @ "image artwork"].
UIImage * imgMask=[UIImage imageNamed: @ "a_41_2"].
UIImageView * imgView=[[UIImageView alloc] initWithFrame: CGRectMake (20, 20, 300, 300)].
ImgView. Layer. BorderWidth=3;
ImgView. BackgroundColor=[UIColor whiteColor];
ImgView. Image=[ViewController maskImage: imgSource withMask: imgMask];
[the self view addSubview: imgView];

}
+ (UIImage *) maskImage: UIImage *) originImage toPath: (UIBezierPath *) path {
UIGraphicsBeginImageContextWithOptions (originImage. Size, NO, 0);
[path addClip];
[originImage drawAtPoint: CGPointZero];
UIImage * img=UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
Return img.
}


+ (UIImage *) maskImage: UIImage *) image withMask: UIImage *) maskImage {
CGImageRef maskRef=maskImage. CGImage;
CGImageRef mask=CGImageMaskCreate (CGImageGetWidth (maskRef),
CGImageGetHeight (maskRef),
CGImageGetBitsPerComponent (maskRef),
CGImageGetBitsPerPixel (maskRef),
CGImageGetBytesPerRow (maskRef),
CGImageGetDataProvider (maskRef), NULL, false);

CGImageRef sourceImage=[image CGImage];
CGImageRef imageWithAlpha=sourceImage;
//add alpha channel for images that don 't have one (ie GIF, JPEG, etc... )
//this clear from a computational cost
//if (CGImageGetAlphaInfo (sourceImage)==kCGImageAlphaNone) {
//
//imageWithAlpha=CopyImageAndAddAlphaChannel (sourceImage);
//}

CGImageRef masked=CGImageCreateWithMask (imageWithAlpha, mask);
CGImageRelease (mask);

//release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel
If (sourceImage!={imageWithAlpha)
CGImageRelease (imageWithAlpha);
}

UIImage * retImage=[UIImage imageWithCGImage: masked];
CGImageRelease (masked);

Return retImage;
}
//the following is the code for the resize images

//the resize the the original image and return a new UIImage object
+ (UIImage *) resizeImage: UIImage *) image size: CGSize newSize {
UIGraphicsBeginImageContext (newSize);
[image drawInRect: CGRectMake (0, 0, newSize. Width, newSize. Height)];
UIImage * newImage=UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
Return newImage;
}
@ end
  •  Tags:  
  • iOS
  • Related