問題:需要將效果圖片中的人物圖片周圍畫一個自定義的畫框,比如說腳丫子,梅花圖案等等,邊框中間鏤空后顯示人物圖片.
查看網路解決方案只有一個"CAShapeLayer和UIBezierPath",但是這些方案我做不到指定的不規則圖案.請問大神如何解決?
邊框圖案:


效果圖片:
uj5u.com熱心網友回復:
解決方案:使用蒙板即可.//
// 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原圖"];
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];
[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 however has 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;
}
//下面的是resize圖片的代碼
// resize 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/43952.html
標籤:iOS
上一篇:android sqlite資料庫 no such column: number (code 1 SQLITE_ERROR):
