gpt4 book ai didi

ios - 如何在保存到 PNG 的 Swift 中的 .WriteToFile 的图像顶部插入标签

转载 作者:可可西里 更新时间:2023-11-01 00:59:23 24 4
gpt4 key购买 nike

我正在尝试使用 .WriteToFile 在图像顶部保存标签。

这是我用来保存图像的代码:

    let selectedImage: UIImage = image.image!
let fileManager = NSFileManager.defaultManager()
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let filePathToWrite = "\(paths)/User_Profile_Image.png"
let imageData: NSData = UIImagePNGRepresentation(selectedImage)!
let jpgImageData = UIImageJPEGRepresentation(selectedImage, 1.0)
fileManager.createFileAtPath(filePathToWrite, contents: jpgImageData, attributes: nil)
// Check file saved successfully
let getImagePath = (paths as NSString).stringByAppendingPathComponent("User_Profile_Image.png")
if (fileManager.fileExistsAtPath(getImagePath)) {
print("FILE AVAILABLE");
} else {
print("FILE NOT AVAILABLE");
}

但它只保存图像:screenshot of simulator and image saved

我正在尝试通过以下方式检索它:

 if let pdfURL = NSBundle.mainBundle().URLForResource("User_Profile_Image", withExtension: "png", subdirectory: nil, localization: nil),data = NSData(contentsOfURL: pdfURL), baseURL = pdfURL.URLByDeletingLastPathComponent  {
let webView = UIWebView(frame: CGRectMake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40))
print("Hello World")
webView.loadData(data, MIMEType: "application/pdf", textEncodingName:"", baseURL: baseURL)//application/
self.view.addSubview(webView)
}

但它不起作用。有什么想法吗?

最佳答案

在您当前的方法中,您将 UILabel 放置在图像的顶部,这不会以任何方式修改图像,标签只是位于图像上方。要在图像上实际写入文本,您需要使用 Graphics 上下文在图像上实际绘制文本。

看看下面的代码,这将拍摄一张图片并在右下角用白色文本写下 Hello World。

class ViewController: UIViewController {

@IBOutlet weak var imageVIew: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

let font = UIFont.boldSystemFontOfSize(30)
let showText:NSString = "hello world"
// setting attr: font name, color...etc.
let attr = [NSFontAttributeName: font, NSForegroundColorAttributeName:UIColor.whiteColor()]
// getting size
let sizeOfText = showText.sizeWithAttributes(attr)

let image = UIImage(named: "dog")
let rect = CGRectMake(0, 0, image!.size.width, image!.size.height)

UIGraphicsBeginImageContextWithOptions(CGSize(width: rect.size.width, height: rect.size.height), true, 0)

// drawing our image to the graphics context
image?.drawInRect(rect)
// drawing text
showText.drawInRect(CGRectMake(rect.size.width-sizeOfText.width-10, rect.size.height-sizeOfText.height-10, rect.size.width, rect.size.height), withAttributes: attr)

// getting an image from it
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()

self.imageVIew.image = newImage
}
}

完成此操作后,您可以将新图像另存为文件,或覆盖现有图像。

我用了这个guide的底部作为引用,我已经有一段时间没有做这样的事情了

关于ios - 如何在保存到 PNG 的 Swift 中的 .WriteToFile 的图像顶部插入标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37500014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com