Я хочу присвоить переменную несколько значений. Я понятия не имею, как это сделать. В принципе, я хочу поместить несколько изображений на изображение.Несколько назначений на Swift
Это единый код присваивания (который работает отлично):
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageDisplay.image = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint(x: 400, y: 300))
}
dismissViewControllerAnimated(true, completion: nil)
}
Я просто хочу, чтобы присвоить большую ценность для ImageDisplay.image. Здесь я могу показать (неправильный) пример:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageDisplay.image = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint(x: 400, y: 100))
ImageDisplay.image = textToImage("HERE IS SECOND LABEL", inImage: image, atPoint: CGPoint(x: 400, y: 200))
ImageDisplay.image = textToImage("HERE IS THIRD LABEL", inImage: image, atPoint: CGPoint(x: 400, y: 300))
}
dismissViewControllerAnimated(true, completion: nil)
}
Это textToImage функция:
func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{
// Setup the font specific variables
let textColor: UIColor = UIColor.blackColor()
let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 200)!
//Setup the image context using the passed image.
UIGraphicsBeginImageContext(inImage.size)
//Setups up the font attributes that will be later used to dictate how the text should be drawn
let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
]
//Put the image into a rectangle as large as the original image.
inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))
// Creating a point within the space that is as bit as the image.
let rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)
//Now Draw the text into an image.
drawText.drawInRect(rect, withAttributes: textFontAttributes)
// Create a new image out of the images we have created
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
// End the context now that we have the image we need
UIGraphicsEndImageContext()
//And pass it back up to the caller.
return newImage
}
где textToImage? –
@UmairAfzal Я обновил функцию textToImage – coskukoz