Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift的UIImageView中添加文本?_Ios_Iphone_Swift_Uiimageview - Fatal编程技术网

Ios 如何在Swift的UIImageView中添加文本?

Ios 如何在Swift的UIImageView中添加文本?,ios,iphone,swift,uiimageview,Ios,Iphone,Swift,Uiimageview,我在编辑UIImageView时遇到一些问题。用户可以在imageview中输入文本,然后进行保存处理,然后用户可以使用文本查看此图像 我如何才能做到这一点?请尝试以下方法 func waterMarkedImage(waterMarkText:String, corner:WaterMarkCorner = .BottomRight, margin:CGPoint = CGPoint(x: 20, y: 20), waterMarkTextColor:UIColor = UIColor.whi

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

我如何才能做到这一点?

请尝试以下方法

func waterMarkedImage(waterMarkText:String, corner:WaterMarkCorner = .BottomRight, margin:CGPoint = CGPoint(x: 20, y: 20), waterMarkTextColor:UIColor = UIColor.whiteColor(), waterMarkTextFont:UIFont = UIFont.systemFontOfSize(20), backgroundColor:UIColor = UIColor.clearColor()) -> UIImage{

    let textAttributes = [NSForegroundColorAttributeName:waterMarkTextColor, NSFontAttributeName:waterMarkTextFont]
    let textSize = NSString(string: waterMarkText).sizeWithAttributes(textAttributes)
    var textFrame = CGRectMake(0, 0, textSize.width, textSize.height)

    print(textSize.height)

    var imageSize = self.size
    imageSize = CGSize(width: imageSize.width, height: (imageSize.height+textSize.height))
    switch corner{
    case .TopLeft:
        textFrame.origin = margin
    case .TopRight:
        textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x, y: margin.y)
    case .BottomLeft:
        textFrame.origin = CGPoint(x: margin.x, y: imageSize.height - textSize.height - margin.y)
    case .BottomRight:
        textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x, y: imageSize.height - textSize.height - margin.y)
    case .Center:
        textFrame.origin = CGPoint(x: imageSize.width/2 - textSize.width/2, y: imageSize.height - textSize.height - margin.y)
    }

    /// Start creating the image with water mark
    //imageSize = CGSize(width: (imageSize.width+textSize.width), height: (imageSize.height+textSize.height))
    UIGraphicsBeginImageContext(imageSize)
    self.drawInRect(CGRectMake(0, 0, imageSize.width, imageSize.height - textSize.height))
    NSString(string: waterMarkText).drawInRect(textFrame, withAttributes: textAttributes)

    let waterMarkedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return waterMarkedImage
}

我尝试并得到了在UIImageview中添加文本的解决方案

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)")
    }

}
}

请包含相关代码以帮助我们帮助您。马尼坎丹查看下面的我的答案。它工作正常。请尝试让我知道。请告诉我否决我的答案的原因。我的答案正确,请尝试我的答案。如果我的答案不工作,我将接受否决投票。这完全正确。