我正在开发一个应用程序,它允许用户将最多 3 个图像附加到放置的 map 图钉上。目前我有一个小预览来查看附加图像,这些图像显示在 UIView 上。我试图让用户在按下预览按钮后查看全屏图像。以下是图像预览按钮的示例:
相关代码如下:
// Image preview
img1 = UIButton(frame:TCRectMake(x: 18,y:82,width:80,height:110))
img1.backgroundColor = UIColor.clearColor()
img1.contentMode = .ScaleAspectFill
img1.clipsToBounds = true
img1.addTarget(self, action:"selectImage:", forControlEvents: UIControlEvents.TouchUpInside)
popupView.addSubview(img1)
img2 = UIButton(frame:TCRectMake(x: 100.8,y:82,width:80,height:110))
img2.backgroundColor = UIColor.clearColor()
img2.contentMode = .ScaleAspectFill
img2.clipsToBounds = true
img2.addTarget(self, action:"selectImage:", forControlEvents: UIControlEvents.TouchUpInside)
popupView.addSubview(img2)
img3 = UIButton(frame:TCRectMake(x: 183,y:82,width:80,height:110))
img3.backgroundColor = UIColor.clearColor()
img3.contentMode = .ScaleAspectFill
img3.clipsToBounds = true
img3.addTarget(self, action:"selectImage:", forControlEvents: UIControlEvents.TouchUpInside)
popupView.addSubview(img3)
...
// Save image to document
func saveImageIntoDocument(resizeImage:UIImage,saveCompleted:((String)->())? = nil)
{
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let timeStamp = "\((Int(Timestamp))).png"
let filePath = documentsUrl.relativePath!.stringByAppendingString("/\(timeStamp)")
if saveCompleted != nil
{
saveCompleted!(timeStamp)
}
UIImagePNGRepresentation(fixRotation(resizeImage))!.writeToFile(filePath, atomically: true)
}
这是每个预览按钮的代码:
func image1Click(sender:UIButton)
{
print(sender.tag)
}
func image2Click(sender:UIButton)
{
print(sender.tag)
}
func image3Click(sender:UIButton)
{
print(sender.tag)
}
我是 Swift 新手,所以如果我错过了什么,请告诉我:) 如果有人可以帮助我全屏查看预览,那就太好了!
最佳答案
这应该可以帮助你。此代码使用手势,如果您单击图像本身,那么它会进入全屏,但您至少可以从中获取一些提示。
它的工作原理是创建一个涵盖所有内容的新图像。它有 TapGestureRecognizer
,可以从其 superView 中隐藏全屏图像。
@IBAction func imgTapped(sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .blackColor()
newImageView.contentMode = .ScaleAspectFit
newImageView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}
func dismissFullscreenImage(sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
关于swift - 按下按钮后全屏查看图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38255818/