現在このようにして、画像とテキストを合成しました
現在やりたいこととしては、決まっている画像があり、そこに日にちを表示させるようにしたいです。どのようにすればよろしいでしょうか?
swift初心者なためわからないことが多いと思いますがご回答よろしくお願いします
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let text = "時間" let font = UIFont.boldSystemFont(ofSize: 100) // Screen Size の取得 let screenWidth:CGFloat = view.frame.size.width let screenHeight:CGFloat = view.frame.size.height // Emmy画像の UIImage インスタンスを生成 let imageEmmy:UIImage! = UIImage(named:"sample1") // Emmy画像の幅・高さの取得 let imageWidth = imageEmmy.size.width let imageHeight = imageEmmy.size.height // 描画領域を生成 let rect = CGRect(x:0, y:0, width:imageWidth, height:imageHeight) // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Context 開始 // 一番大きい画像サイズでContextを開く UIGraphicsBeginImageContext(imageEmmy.size) // Retinaで画像が粗い場合 //UIGraphicsBeginImageContextWithOptions(imageEmmy.size, false, 0) // EmmyをUIImageのdrawInRectメソッドでレンダリング imageEmmy.draw(in: rect) // テキストの描画領域 let textRect = CGRect(x:100, y:imageHeight/2+40, width:imageWidth-250, height:120) let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle let textFontAttributes = [ NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.paragraphStyle: textStyle ] // テキストをdrawInRectメソッドでレンダリング text.draw(in: textRect, withAttributes: textFontAttributes) // Context に描画された画像を新しく設定 let newImage = UIGraphicsGetImageFromCurrentImageContext(); // Context 終了 UIGraphicsEndImageContext() // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // UIImageView インスタンス生成 let imageView = UIImageView() imageView.image = newImage // 画像サイズをスクリーン幅に合わせる, scaling let scale = screenWidth / imageWidth let newRect = CGRect(x:0, y:0, width:imageWidth*scale, height:imageHeight*scale) // ImageView frame をCGRectMakeで作った矩形に合わせる imageView.frame = newRect // 画像の中心をスクリーンの中心にする imageView.center = CGPoint(x:screenWidth/2, y:screenHeight/2) // view に ImageView を追加する self.view.addSubview(imageView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
日付を表示するコード
let now = Date() let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "ja_JP") dateFormatter.dateFormat = "MM月dd日E曜日"
追記
決まった画像と日にちを合成させた後の、画像をカメラロールに保存できるようにしたいので、画像とlabelが別々に別れないほうが好ましいです。
結局のところ、質問の内容は何ですか?
決まっている画像に、変動するテキスト(今回は日にち)を入れるようにしたいです。
画像の上にUILabelを乗せるのではダメなのでしょうか?
あと、 https://teratail.com/questions/92881 この質問と内容は違うのでしょうか?同じ質問なのであればどちらかに削除依頼を出して下さい。
失礼いたしました、依頼を出しておきました。
画像と日にちを合成させた後の画像をカメラロールに保存できるようにしたいので、このような質問をさせて頂きました。質問がわかりずらくすいません。
回答1件
あなたの回答
tips
プレビュー