美文网首页
iOS 图片相关处理

iOS 图片相关处理

作者: T_label | 来源:发表于2017-02-07 17:11 被阅读21次

    图片合成

    -(UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
    UIGraphicsBeginImageContext(image1.size);
    // Draw image1
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
    // Draw image2
    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
    }

    图片拼接(左右拼接)

    -(UIImage *) combine:(UIImage *)leftImage :(UIImage )rightImage {
    CGFloat width = leftImage.size.width ;
    CGFloat height = leftImage.size.height;
    CGSize offScreenSize = CGSizeMake(width, height);
    UIGraphicsBeginImageContext(offScreenSize);
    CGRect rect = CGRectMake(0, 0, width, height);
    [leftImage drawInRect:rect];
    rect.origin.y += height;
    [rightImage drawInRect:rect];
    UIImage
    imagez = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imagez;
    }

    图片拼接(上下拼接,以第一张图片的宽度为主)

    -(UIImage *)combine:(UIImage *)oneImage otherImage:(UIImage *)otherImage {
    //计算画布大小
    CGFloat width = oneImage.size.width;
    CGFloat height = oneImage.size.height + otherImage.size.height+30;
    CGSize resultSize = CGSizeMake(width, height);
    UIGraphicsBeginImageContextWithOptions(resultSize,nil, 0.0);
    //放第一个图片
    CGRect oneRect = CGRectMake(0, 0, resultSize.width, oneImage.size.height);
    [oneImage drawInRect:oneRect];
    //放第二个图片
    CGRect otherRect = CGRectMake(0, oneRect.size.height, resultSize.width, otherImage.size.height+30);
    [otherImage drawInRect:otherRect];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
    }

    通过颜色来生成一个纯色图片

    -(UIImage *)buttonImageFromColor:(UIColor *)color{
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
    }

    tableView截图

    -(UIImage *)getTableViewimage:(UITableView )tableview{
    UIImage
    viewImage = nil;
    UITableView *scrollView = tableview;
    UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, 0.0);
    {
    CGPoint savedContentOffset = scrollView.contentOffset;
    CGRect savedFrame = scrollView.frame;
    scrollView.contentOffset = CGPointZero;
    scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
    [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
    viewImage = UIGraphicsGetImageFromCurrentImageContext();
    scrollView.contentOffset = savedContentOffset;
    scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();
    return viewImage;
    }

    相关文章

      网友评论

          本文标题:iOS 图片相关处理

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