如何在Swift中的UIImageView中添加文本? [英] How to add the text in UIImageView in Swift?

查看:168
本文介绍了如何在Swift中的UIImageView中添加文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编辑UIImageView时遇到一些问题.用户可以在imageview中输入文本,然后进行处理以保存它,然后该用户可以使用文本查看该图像.

I have some issues for editing UIImageView. User can enter the text in imageview then it processes to save it and after that user can view this image with the text .

我该怎么做?

推荐答案

我试图找到在UIImageview中添加文本的解决方案.下面是编码

I tried and got the solution for adding the text inside the UIImageview.Below is the coding

import UIKit

class ViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate,UIGestureRecognizerDelegate{

@IBOutlet var imageViewText: UIImageView!
var dynamicTextViewInsideImageView : UITextView!
var strImageSelected : String!
var picker = UIImagePickerController()
var xValue = CGFloat()
var yValue = CGFloat()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    imageViewText.layer.cornerRadius = 5
    imageViewText.layer.borderColor = UIColor.blueColor().CGColor
    imageViewText.layer.borderWidth = 1
    strImageSelected = ""
}

override func viewWillAppear(animated: Bool)
{
    let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
    //Add the recognizer to your view.
    imageViewText.userInteractionEnabled = true
    tapRecognizer.numberOfTapsRequired = 1
    imageViewText.addGestureRecognizer(tapRecognizer)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func actionPickImage(sender: AnyObject)
{
    var alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    var cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
        {
            UIAlertAction in
            self.openCamera()
    }
    var gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertActionStyle.Default)
        {
            UIAlertAction in
            self.openGallary()
    }
    var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
        {
            UIAlertAction in
    }

    // Add the actions
    picker.delegate = self
    alert.addAction(cameraAction)
    alert.addAction(gallaryAction)
    alert.addAction(cancelAction)
    self.presentViewController(alert, animated: true, completion: nil)

}


func openCamera()
{
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
    {
        picker.sourceType = UIImagePickerControllerSourceType.Camera
        self .presentViewController(picker, animated: true, completion: nil)
    }
    else
    {
        let alertWarning = UIAlertView(title:"Warning", message: "You don't have camera", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:"")
        alertWarning.show()
    }
}
func openGallary()
{
    picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    self.presentViewController(picker, animated: true, completion: nil)
}

//PickerView Delegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
{
    picker .dismissViewControllerAnimated(true, completion: nil)
    imageViewText.image=info[UIImagePickerControllerOriginalImage] as? UIImage
    strImageSelected = "ImagePicked"
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
    println("picker cancel.")
    strImageSelected = ""
}
//On the imageView you can add text(Only after you picked the image from Gallery or Camera) 
func imageTapped(gestureRecognizer: UITapGestureRecognizer)
{
    if strImageSelected.isEmpty{
       println("Do not add the textview inside imageview as you have not picked the image from gallery or camera")
    }
    else
    {
         println("you picked the image successfully")
        dynamicTextViewInsideImageView = UITextView(frame: CGRectMake(xValue,yValue,200,50))
        dynamicTextViewInsideImageView.backgroundColor = UIColor( red: 0.9, green: 0.9, blue:0.9, alpha: 1.0 )
        imageViewText.addSubview( dynamicTextViewInsideImageView)
    }

}
//TouchEvent for Getting X,Y position once we touch inside the imageview
override func  touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
    if let touch = touches.anyObject() as? UITouch
    {
      let location = touch.locationInView(imageViewText) as CGPoint
      println("the location.x is - \(location.x)")
      println("the location.y is - \(location.y)")
      xValue = location.x
      yValue = location.y
      println("the xValue is - \(xValue)")
      println("the yValue is - \(yValue)")
    }

}
}

这篇关于如何在Swift中的UIImageView中添加文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆