iOS 将两张image合并成一张image

发布于 2022-09-01 15:32:22 字数 326 浏览 20 评论 0

viewController 中有两个imageView,其中一张有倾斜(imageView的transform属性发生改变),将这两张image合成一张,保持相对的位置不变。图片描述

效果大致如下图(第二张的位置有问题)
图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贵在坚持 2022-09-08 15:32:22

设置第二张imageView.layer.zPosition = CGPointMake(.5, 1);
注意frame会改变 自己setFrame一下 这个时候frame的参考点变成下边中间的点了

山田美奈子 2022-09-08 15:32:22

有些乱但最终还是解决了实际的问题!
- (UIImage *)saveImageWithUIImage:(UIImage *)srcImage{
//要绘制的实际image
UIImage *mergeImage = srcImage;

UIGraphicsBeginImageContext(mergeImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();
/*
 *背景image绘制到上下文中
 */
[mergeImage drawInRect:(CGRect){0, 0, mergeImage.size.width, mergeImage.size.height}];
//imageView上的所有子视图
for (UIView *view  in [self.baseView subviews]) {
    if ([view isKindOfClass:[ZDStickerView class]]) {
        ZDStickerView *stickerView = (ZDStickerView *)view;
        /*
         * step_1.1 :移动坐标中心点至要绘制view的中心
         */
        CGPoint translateCenter = [self getTranslatePositionWithImageSize:mergeImage.size imageViewSize:self.baseView.bounds.size originPosition:stickerView.layer.position contentModle:self.baseView.contentMode];
        CGContextTranslateCTM(context, translateCenter.x, translateCenter.y);
        /*
         * step_1.2 :移动坐标中心点至要绘制view的左上角
         */
        CGPoint traslateCorner = [self getTranslateCornerPositionWithScaleRate:[self getScaleRateWithImageSize:mergeImage.size imageViewSize:self.baseView.bounds.size contentMode:self.baseView.contentMode] layerSize:stickerView.endScaleSize transformAngle:stickerView.endDeltaAngle];
        CGContextTranslateCTM(context, traslateCorner.x, traslateCorner.y);
        /*
         * step_2 : 旋转坐标
         */
        CGContextRotateCTM(context, stickerView.endDeltaAngle);

        /*
         * step_3 : 绘制
         */
        CGSize t_size = [self getTranslateSizeWithImageSize:mergeImage.size imageViewSize:self.baseView.bounds.size originSize:stickerView.endScaleSize contentMode:self.baseView.contentMode];
        UIImageView *drawImageView = (UIImageView *)stickerView.contentView;
        [drawImageView.image drawInRect:(CGRect){0, 0, t_size.width, t_size.height}];
        /*
         * step_4 : 恢复坐标系
         */
        CGContextRotateCTM(context, -stickerView.endDeltaAngle);
        CGContextTranslateCTM(context, -traslateCorner.x, -traslateCorner.y);
        CGContextTranslateCTM(context, -translateCenter.x, -translateCenter.y);
    }
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

}

  • (float)getScaleRateWithImageSize:(CGSize)imageSize imageViewSize:(CGSize)viewSize contentMode:(UIViewContentMode)mode{
    if (mode == UIViewContentModeScaleAspectFill) {
    return [self getScaleRateWithImageSize:imageSize imageViewSize:viewSize];
    }else{
    float viewRate = viewSize.width / viewSize.height;
    float imageRate = imageSize.width / imageSize.height;
    float rate = 1.0f;
    /*
    * image按竖直方上的比率压缩
    /
    if (viewRate > imageRate) {
    rate = imageSize.height / viewSize.height;
    }
    /

    * image按水平方上的比率压缩
    */
    else{
    rate = imageSize.width / viewSize.width;
    }
    return rate;

    }
    return 1.0;
    }

  • (float)getScaleRateWithImageSize:(CGSize)imageSize imageViewSize:(CGSize)viewSize{
    float viewRate = viewSize.width / viewSize.height;
    float imageRate = imageSize.width / imageSize.height;
    float rate = 1.0f;
    /*

    • image的宽按比例缩放,竖向上得到裁剪
      /
      if (viewRate > imageRate) {
      rate = imageSize.width / viewSize.width;
      }
      /
    • image的高按比例缩放,横向上得到裁剪
      /
      else{
      rate = imageSize.height / viewSize.height;
      }
      return rate;
      }
      /

      *新坐标系下甲片左上角的坐标
      */
  • (CGPoint)getTranslateCornerPositionWithScaleRate:(float)rate layerSize:(CGSize)layerSize transformAngle:(float)angle{
    float originAngle = [self programWithWidth:layerSize.width height:layerSize.height];
    float halfDiagonalLength = [self halfDiagonalLengthWithWidth:layerSize.width height:layerSize.height] * rate;
    float x = halfDiagonalLength * cosf(originAngle - angle);
    float y = halfDiagonalLength * sinf(originAngle - angle);
    return (CGPoint){x, -y};
    }
    /*
    *甲片中心在绘图上下文中的坐标 UIViewContentModeScaleAspectFit / UIViewContentModeScaleAspectFill
    */
  • (CGPoint)getTranslatePositionWithImageSize:(CGSize)imageSize imageViewSize:(CGSize)viewSize originPosition:(CGPoint)position contentModle:(UIViewContentMode)mode{
    if (mode == UIViewContentModeScaleAspectFill) {
    return [self getTranslatePositionWithImageSize:imageSize imageViewSize:viewSize originPosition:position];
    }else{// --- UIViewContentModeScaleAspectFit ---
    float viewRate = viewSize.width / viewSize.height;
    float imageRate = imageSize.width / imageSize.height;

    float position_x = 0.0f;
    float position_y = 0.0f;
    /*
     * image按竖直方上的比率压缩
     */
    if (viewRate > imageRate) {
        float actualViewWidth = imageSize.width * (viewSize.height / imageSize.height);
        float distanceToLeft = (actualViewWidth - viewSize.width) * 0.5;
        position_x = position.x + distanceToLeft;
        position_y = position.y;
        position_x *= imageSize.height / viewSize.height;
        position_y *= imageSize.height / viewSize.height;
    }
    /*
     * image按水平方上的比率压缩
     */
    else{
        float actualViewHeight = imageSize.height * (viewSize.width / imageSize.width);
        float distanceToTop = (actualViewHeight - viewSize.height) * 0.5;
        position_x = position.x;
        position_y = position.y + distanceToTop;
        position_x *= imageSize.width / viewSize.width;
        position_y *= imageSize.width / viewSize.width;
    }
    return (CGPoint){position_x,position_y};
    

    }
    return CGPointZero;
    }

  • (CGPoint)getTranslatePositionWithImageSize:(CGSize)imageSize imageViewSize:(CGSize)viewSize originPosition:(CGPoint)position{
    float viewRate = viewSize.width / viewSize.height;
    float imageRate = imageSize.width / imageSize.height;

    float position_x = 0.0f;
    float position_y = 0.0f;
    /*

    • image的宽按比例缩放,竖向上得到裁剪
      /
      if (viewRate > imageRate) {
      float actualViewHeight = imageSize.height * (viewSize.width / imageSize.width);
      float deltaHeight = (actualViewHeight - viewSize.height) * 0.5;
      position_x = position.x;
      position_y = position.y + deltaHeight;
      position_x *= imageSize.width / viewSize.width;
      position_y *= imageSize.width / viewSize.width;
      }
      /
    • image的高按比例缩放,横向上得到裁剪
      */
      else{
      float actualViewWidth = imageSize.width * (viewSize.height / imageSize.height);
      //此时actualViewWidth 应大于 viewSize.width
      float deltaWidth = (actualViewWidth - viewSize.width) * 0.5;
      position_x = position.x + deltaWidth;
      position_y = position.y;
      position_x *= imageSize.height / viewSize.height;
      position_y *= imageSize.height / viewSize.height;
      }
      return (CGPoint){position_x,position_y};
      }
  • (CGSize)getTranslateSizeWithImageSize:(CGSize)imageSize imageViewSize:(CGSize)viewSize originSize:(CGSize)size contentMode:(UIViewContentMode)mode{
    if (mode == UIViewContentModeScaleAspectFill) {
    return [self getTranslateSizeWithImageSize:imageSize imageViewSize:viewSize originSize:size];
    }else{
    float viewRate = viewSize.width / viewSize.height;
    float imageRate = imageSize.width / imageSize.height;

    float size_w = size.width;
    float size_h = size.height;
    /*
     * image的宽按比例缩放,竖向上得到裁剪
     */
    if (viewRate > imageRate) {
        float rate = imageSize.height / viewSize.height;
        size_w *= rate;
        size_h *= rate;
    }
    /*
     * image的高按比例缩放,横向上得到裁剪
     */
    else{
        float rate = imageSize.width / viewSize.width;
        size_w *= rate;
        size_h *= rate;
    }
    return (CGSize){size_w,size_h};
    

    }

}

/*
*甲片在绘图上下文中的尺寸
*/
- (CGSize)getTranslateSizeWithImageSize:(CGSize)imageSize imageViewSize:(CGSize)viewSize originSize:(CGSize)size{
float viewRate = viewSize.width / viewSize.height;
float imageRate = imageSize.width / imageSize.height;

float size_w = size.width;
float size_h = size.height;
/*
 * image的宽按比例缩放,竖向上得到裁剪
 */
if (viewRate > imageRate) {
    float rate = imageSize.width / viewSize.width;
    size_w *= rate;
    size_h *= rate;
}
/*
 * image的高按比例缩放,横向上得到裁剪
 */
else{
    float rate = imageSize.height / viewSize.height;
    size_w *= rate;
    size_h *= rate;
}
return (CGSize){size_w,size_h};

}
//对角线与x轴的夹角
- (float)programWithWidth:(float)w height:(float)h{
float p = [self halfDiagonalLengthWithWidth:w height:h];
float dis = 0 - w * 0.5;
float dis_p = dis / p;
return acosf(dis_p);
}

//对角线的一半
- (float)halfDiagonalLengthWithWidth:(float)w height:(float)h{
return sqrtf(w * w / 4.0 + h * h / 4.0);
}

情痴 2022-09-08 15:32:22

楼主你好,我想问一下那个self.baseView是什么类啊(自定义的?),stickerView.endScaleSize,stickerView.endDeltaAngle的属性哪里来的啊,我下载ZDStickerView里面没有啊,谢谢啊!

半窗疏影 2022-09-08 15:32:22

两张image旋转合成 有demo嘛 大神 发我一份吧 谢谢 啦

    824867471@qq.com     不胜感激
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文