gpt4 book ai didi

ios - 向图像 Auk lib 添加描述

转载 作者:行者123 更新时间:2023-11-30 12:34:49 25 4
gpt4 key购买 nike

我正在使用 Auk lib 在 slider 中呈现我的图像,我需要为每个图像设置描述。我怎样才能简单地做到这一点?现在我尝试下载图像,然后在其上设置描述:

    override func viewDidLoad() {
super.viewDidLoad()

for picture in LogInViewController.dictionaryForSlider
{
//scrollView.auk.show(url: picture.key, accessibilityLabel: picture.value)
let downloadedPic: UIImage = self.downloadImage(urlStr: picture.key)
let newPic: UIImage = self.textToImage(drawText: picture.value as NSString, inImage: downloadedPic, atPoint: CGPoint(x: 10, y: 10))

scrollView.auk.show(image: newPic)
}

}


func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold", size: 12)!

let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)

let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
] as [String : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))

let rect = CGRect(origin: point, size: image.size)
text.draw(in: rect, withAttributes: textFontAttributes)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage!
}

func downloadImage(urlStr: String) -> UIImage {
var img: UIImage? = nil
var imgURL: NSURL = NSURL(string: urlStr)!
let request: NSURLRequest = NSURLRequest(url: imgURL as URL)
NSURLConnection.sendAsynchronousRequest(
request as URLRequest, queue: OperationQueue.main,
completionHandler: { response,data,error in
if error == nil {
img = UIImage(data: data!)
}
})
return img!
}

但是我收到了这个错误:

2017-03-25 08:02:04.627615 app1[314:25451] fatal error: unexpectedly found nil while unwrapping an Optional value

在此字符串上:

return img!

我该如何解决这个问题?或者还有其他简单的方法来为图像设置描述吗?

最佳答案

我已经处理了它,没有任何额外的库/GB 代码 XD 只需使用 UIScrollViewDelegate

import UIKit
import Auk

class SliderViewController: UIViewController, UIScrollViewDelegate {


@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var labelForInfo: UILabel!
@IBOutlet weak var navBarTitle: UINavigationItem!

var lastPoint: CGPoint?
var listForDescr: [Int : String] = [Int : String]()

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
lastPoint = scrollView.contentOffset
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if (lastPoint?.x)! < scrollView.contentOffset.x {
//print(scrollView.auk.currentPageIndex ?? 0)
navBarTitle.title = listForDescr[scrollView.auk.currentPageIndex ?? 0]
}
else if (lastPoint?.x)! > scrollView.contentOffset.x {
//print(scrollView.auk.currentPageIndex ?? 0)
navBarTitle.title = listForDescr[scrollView.auk.currentPageIndex ?? 0]
}
}

override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.delegate = self

labelForInfo.text = ""
scrollView.backgroundColor = UIColor.white
var count: Int = 0
for picture in LogInViewController.dictionaryForSlider
{
scrollView.auk.show(url: picture.key, accessibilityLabel: picture.value)
listForDescr.updateValue(picture.value, forKey: count)
count+=1
}

}


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

}

关于ios - 向图像 Auk lib 添加描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013135/

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