美文网首页
iOS分享页面图片遮盖组合效果实现

iOS分享页面图片遮盖组合效果实现

作者: 数字d | 来源:发表于2021-09-02 13:57 被阅读0次

需求:二维码是根据下载链接自己生成的小图
另外一张是UI做的背景图,上面有文字和留白区域,给放二维码留了特定的位置和大小

二维码图例:

1in.png

背景图例:


1.png

需求:需要将二维码图片放置在背景图的黄色框框位置,组合出来的图片可根据屏幕大小自动填充,可保存到本地相册

注意点

1.因为背景图上包含文字,所以实现图片组合的时候需要根据背景图的实际大小来进行组合,防止背景图上的文字被拉伸,变模糊
2.在组合完了图片以后进行图片展示到view上的时候,需要对组合后的图片视图进行缩放来适配页面的宽度

实现步骤

1.下载链接生成二维码图片

目测这里生成的二维码图片不是高清的,还有更高清的方法来实现,以后找到了在这里补充

-(UIImage *)generaterSmallErcode{
    
    // 1.实例化二维码滤镜
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    
    [filter setDefaults];
    
    // 3.将字符串转换成NSdata
    
    NSData *data  = [@"http://192.168.0.26:8081/#/mobile/download" dataUsingEncoding:NSUTF8StringEncoding];
    
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    
    // 5.生成二维码
    
    CIImage *outputImage = [filter outputImage];
    UIImage *image = [UIImage imageWithCIImage:outputImage];
    
    return image;
    
}

2.将两张图放在同一个view上做展示

-(UIView *)totalBgView {
    if (_totalBgView == nil) {
        _totalBgView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 630)];
        [_totalBgView addSubview:self.bgImageView];
        [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(_totalBgView);
        }];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(375 - 55.5 - 25.5, 630 - 17 - 55.5, 55.5, 55.5)];
        imageView.image = [self generaterSmallErcode];
        [_totalBgView addSubview:imageView];
    }
    return _totalBgView;
}

-(UIImageView *)bgImageView {
    if (_bgImageView == nil) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageNamed:@"yz_icon_icon_drawing"];
        _bgImageView.contentMode = UIViewContentModeScaleAspectFit;
//        _bgImageView.image = [self composeImg];
    }
    return _bgImageView;
}

3.将最终的展示view做缩放和居中展示,宽高适配

        _rateX = Screen_Width / 375;
        if (_rateX >= 1) {
            self.totalBgView.center = CGPointMake(Screen_Width / 2.0, 630 / 2.0);
        }else {
//            [self.totalBgView setZoomScale:0.5 animated:YES];
            [UIView animateWithDuration:0.01 animations:^{
                self.totalBgView.transform = CGAffineTransformMakeScale(_rateX,_rateX);
                self.totalBgView.center = CGPointMake(Screen_Width / 2.0 , (630 /2.0) * _rateX);
            } completion:^(BOOL finished) {
                [self loadImageFinished:[self convertViewToImage:self.totalBgView]];
            }];
        }

4.将view转换成图片

注意这里需要使用图片的实际宽高,否则会出现裁剪效果,导致部分机型适配后二维码区域被裁剪掉


- (UIImage *)convertViewToImage:(UIView *)view {

    UIImage *imageRet = [[UIImage alloc]init];
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(375, 630), YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    imageRet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageRet;

}

5.将图片存储到本地相册

- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}

整体代码

#import "YZCreatePosterView.h"

@interface YZCreatePosterView()
@property(nonatomic,strong)UIView * blackView;
@property(nonatomic,strong)UIView * totalBgView;
@property(nonatomic,strong)UIImageView * bgImageView;
@property(nonatomic,strong)UIImageView * innerImageView;
@property(nonatomic,assign)CGFloat rateX;
@end

@implementation YZCreatePosterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.blackView];
        
        
        [self addSubview:self.totalBgView];

        _rateX = Screen_Width / 375;
        if (_rateX >= 1) {
            self.totalBgView.center = CGPointMake(Screen_Width / 2.0, 630 / 2.0);
        }else {
//            [self.totalBgView setZoomScale:0.5 animated:YES];
            [UIView animateWithDuration:0.01 animations:^{
                self.totalBgView.transform = CGAffineTransformMakeScale(_rateX,_rateX);
                self.totalBgView.center = CGPointMake(Screen_Width / 2.0 , (630 /2.0) * _rateX);
            } completion:^(BOOL finished) {
                [self loadImageFinished:[self convertViewToImage:self.totalBgView]];
            }];
        }
    }
    
    
    
    return self;
}

-(UIView *)blackView {
    if (_blackView == nil) {
        _blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
        _blackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    }
    return _blackView;
}

-(UIView *)totalBgView {
    if (_totalBgView == nil) {
        _totalBgView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 630)];
        [_totalBgView addSubview:self.bgImageView];
        [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(_totalBgView);
        }];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(375 - 55.5 - 25.5, 630 - 17 - 55.5, 55.5, 55.5)];
        imageView.image = [self generaterSmallErcode];
        [_totalBgView addSubview:imageView];
    }
    return _totalBgView;
}

-(UIImageView *)bgImageView {
    if (_bgImageView == nil) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageNamed:@"yz_icon_icon_drawing"];
        _bgImageView.contentMode = UIViewContentModeScaleAspectFit;
//        _bgImageView.image = [self composeImg];
    }
    return _bgImageView;
}


- (UIImage *)composeImg {
    UIImage *img = [UIImage imageNamed:@"yz_icon_icon_drawing"];
    CGImageRef imgRef = img.CGImage;
//    CGFloat w = CGImageGetWidth(imgRef);
//    CGFloat h = CGImageGetHeight(imgRef);
    
    //以1.png的图大小为底图
    UIImage *img1 = [self generaterSmallErcode];
    CGImageRef imgRef1 = img1.CGImage;
//    CGFloat w1 = CGImageGetWidth(imgRef1);
//    CGFloat h1 = CGImageGetHeight(imgRef1);
    
    //以1.png的图大小为画布创建上下文
    UIGraphicsBeginImageContext(CGSizeMake(375, 630));
    [img drawInRect:CGRectMake(0, 0, 375, 630)];//先把1.png 画到上下文中
    [img1 drawInRect:CGRectMake( 375 - 55.5 - 25.5, 630 - 17 - 55.5, 55.5, 55.5)];//再把小图放在上下文中
    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
    UIGraphicsEndImageContext();//关闭上下文
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"yz_icon_icon_drawing"];
    
    [UIImagePNGRepresentation(resultImg) writeToFile:filePath atomically:YES];//保存图片到沙盒
    CGImageRelease(imgRef);
    CGImageRelease(imgRef1);
    
    return resultImg;
}



-(UIImage *)generaterSmallErcode{
    
    // 1.实例化二维码滤镜
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    
    [filter setDefaults];
    
    // 3.将字符串转换成NSdata
    
    NSData *data  = [@"http://192.168.0.26:8081/#/mobile/download" dataUsingEncoding:NSUTF8StringEncoding];
    
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    
    // 5.生成二维码
    
    CIImage *outputImage = [filter outputImage];
    UIImage *image = [UIImage imageWithCIImage:outputImage];
    
    return image;
    
}

//////使用该方法不会模糊,根据屏幕密度计算
- (UIImage *)convertViewToImage:(UIView *)view {

    UIImage *imageRet = [[UIImage alloc]init];
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(375, 630), YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    imageRet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageRet;

}

- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}

@end

相关文章

网友评论

      本文标题:iOS分享页面图片遮盖组合效果实现

      本文链接:https://www.haomeiwen.com/subject/phaqwltx.html