2

我正在寻找在iOS. 例如,该图像将用于共享到社交网站。

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

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

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

4

3 回答 3

1

我能够在下面创建此图像: 宝丽来

通过以下方法:

  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)
    
于 2016-04-07T19:35:15.507 回答
0

您可以创建 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();
于 2016-04-11T19:49:49.307 回答
0

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

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();
于 2016-04-14T17:41:19.967 回答