2016-04-05 7 views
2

iOSにカスタムイメージを作成する予定です。イメージは、例えばソーシャルサイトへの共有に使用される。UIImageとその他の属性を使用してカスタムイメージを作成する

例として、ユーザーはUIImageという参照を持つ写真を選択することができます。この画像では、その中に含まれているオリジナルの別の画像を作成したいと考えています。

アイデアは、下部に書かれた文章を使ってポラロイド様式のイメージを作成することです。これを達成する最良の方法は何ですか?

私はまず、XIBを作成してレイアウトを制御し、この画面をオフにして、完了したUIImagedeallocを作成するスナップショットを使用します。またはCGContextDrawImageを使用する方が良いでしょうか?我々は特定のレイアウトを必要とする複数のアイテムを持っている場合心配は、レイアウトである私は下のこの画像を作成することができました

+5

広すぎると意見に基づいて – Sulthan

+0

利用可能なオプションとそのメリットについてはどうなりますか?それは意見に基づいているとは言いません。 – StuartM

答えて

1

を達成するために簡単になるだろう、描画コンテキストです:

次のメソッドを介して Polaroid

  1. セットアップポラロイドのようなXIB: Static 鉱山のかなり静的ていますが、簡単に画像やテキストを設定するには、ファイルの所有者や他のいくつかの魔法を使用することができます。

  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. セットアップそうのようなあなたのメインビューコントローラ(画像を 'フェッチ' 1):あなたが作成することができます

    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) 
    
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(); 
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(); 
関連する問題