【问题标题】:Creating a custom image with UIImage and other attributes使用 UIImage 和其他属性创建自定义图像
【发布时间】:2016-04-05 14:10:52
【问题描述】:

我希望在iOS 中创建自定义图像。例如,该图像将用于共享到社交网站。

例如,用户可能会选择一张我们作为UIImage 引用的照片。有了这个图像,我们想要创建另一个图像,其中包含原始图像。

这个想法是创建一个宝丽来风格的图像,底部有一些文字/措辞。实现这一目标的最佳方法是什么?

我的第一个想法是创建一个控制布局的XIB 并在屏幕外初始化,使用快照创建完成的UIImagedealloc 视图。还是使用CGContextDrawImage 会更好?担心的是布局,如果我们有多个需要特定布局的项目,那么上下文绘制将很容易完成

【问题讨论】:

  • 过于宽泛且基于意见
  • 什么选项可用以及它们背后的好处的问题呢?不会说这是基于意见的。

标签: ios swift uiimage cgcontext cgcontextdrawimage


【解决方案1】:

我能够在下面创建此图像: Polaroid

通过以下方法:

  1. 像宝丽来一样设置 XIB: 我的是静态的,但你可以使用文件所有者和其他一些魔法来轻松设置你的图像和文本。

  2. 使用以下内容设置类文件:

    import Foundation
    import UIKit
    
    class LoveIs: UIView {
    
    class func instanceFromNib() -> UIView {
        return UINib(nibName: "Polaroid", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
    }    }
    
  3. 像这样设置您的主视图控制器(一个“获取”图像):

    var loveIs: UIView? = nil
    
    loveIs = LoveIs.instanceFromNib()
    
    loveIs?.layer.borderColor = UIColor.darkGrayColor().CGColor
    loveIs?.layer.borderWidth = 5
    UIGraphicsBeginImageContext((loveIs?.bounds.size)!)
    loveIs?.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
    

【讨论】:

    【解决方案2】:

    您可以创建一个 UIView 并向其添加 UIImageView 和一个 UILabel。然后截取该视图的屏幕截图。

    解决方案 1:

    UIGraphicsBeginImageContext(screenShotView.bounds.size)
    screenShotView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    letscreenShot  = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    

    解决方案 2:

    var screenShot:UIView = screenShotView.snapshotViewAfterScreenUpdates(true)
    UIGraphicsBeginImageContextWithOptions(screenShotView.bounds.size, true, 0.0)
    screenShot.drawViewHierarchyInRect(screenShotView.bounds, afterScreenUpdates: true)
    var shotCapture :UIImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
    

    【讨论】:

      【解决方案3】:

      我建议在代码中生成图像,因为快照屏幕会限制您的屏幕大小。我还发现,当一切都在代码中时,管理复杂的操作会更容易。

      CGContextDrawImage 将帮助您将原始图像复制到新图像的区域。

      下面是一个如何自己创建图像的示例,其中包含居中的文本和形状(在这种情况下,三角形上面带有矩形框,名称居中)。

      // Get font from custom class and create text styles
      UIFont *font = [CBFontHelper robotoMedium:32.0f];
      NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
      paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
      paragraphStyle.alignment = NSTextAlignmentCenter;
      NSDictionary *textAttributes = @{
              NSFontAttributeName: font,
              NSForegroundColorAttributeName: [CBColourHelper white],
      };
      CGSize textSize = [name sizeWithAttributes:textAttributes];
      // various bits of paddings and heights neccessary to calculate total image size
      float gap = 2.0f;
      float textHeight = 0.3f*interfacePadding + textSize.height + 0.3f*interfacePadding;
      float width = 200;
      float height = textHeight + gap + 2*interfacePadding;
      
      //create new image context
      UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height), false, 0.0f);
      // Fill rectangle to hold name
      [[CBColourHelper sandstone] setFill];
      UIRectFill(CGRectMake(0.0f,0.0f,width,textHeight));
      
      //Draw name over rectangle
      [self drawString:name withFont:font inRect:CGRectMake(0.5f*interfacePadding, 0.3f*interfacePadding, width, height)];
      
      // Draw triangle
      [[CBColourHelper sandstone] setFill];
      UIBezierPath *triangle = [UIBezierPath bezierPath];
      [triangle moveToPoint:CGPointMake(width/2,textHeight + gap)];
      [triangle addLineToPoint:CGPointMake(width/2,height)];
      [triangle addLineToPoint:CGPointMake(width/2+5.0f*interfacePadding,textHeight + gap)];
      [triangle closePath];
      [triangle fill];
      
      // Get image from context 
      UIImage *markerImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      

      【讨论】:

        猜你喜欢
        • 2015-10-07
        • 2016-01-12
        • 2020-05-01
        • 2014-09-16
        • 1970-01-01
        • 1970-01-01
        • 2011-09-09
        相关资源
        最近更新 更多