swift - 按下按钮后全屏查看图像

标签 swift image uiview

我正在开发一个应用程序,它允许用户将最多 3 个图像附加到放置的 map 图钉上。目前我有一个小预览来查看附加图像,这些图像显示在 UIView 上。我试图让用户在按下预览按钮后查看全屏图像。以下是图像预览按钮的示例:

enter image description here

相关代码如下:

    // 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/

相关文章:

ios - 如何将 AutoLayout 约束的常量设置为父 View 的百分比?

ios - 如何向 Collection View 单元格的内底边缘添加阴影? swift 3

c++ - 在 devkit pro 中在图像上显示文本时屏幕顶部出现垃圾

c# - 如何在 ASP.NET 和 C# 中的每个 session 中将 "temporarily"图像存储在 Web 服务器上

iphone - 从 nib 加载自定义 UIView,nib 中包含的所有 subview 都是零?

iphone - 在 Objective-C 中将 'safe path' 绘制到 UIImageView 上

swift - 如何在 Swift 中使用 Codable 转换带有可选小数秒的日期字符串?

xcode - CGColorSpaceRef 不是 Swift 中的 AnyObject 吗?

javascript - 为什么这些元素不显示行内 block ?

ios - 为什么 drawRect 会留下部分图像?