按下按钮后全屏查看图像

时间:2016-07-07 21:46:55

标签: swift image uiview

我正在开发一款应用,可让用户将最多3张图片附加到地图引脚上。目前我有一个小预览来查看附加的图像,这些图像显示在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的新手,所以如果我错过任何事情,请告诉我:)如果有人能帮我看全屏预览会很棒!

1 个答案:

答案 0 :(得分:0)

这应该会帮助你。此代码使用手势,如果您单击图像本身,则会进入全屏但您可以从中获取至少一些提示。

它的工作原理是创建一个涵盖所有内容的新图像。它有TapGestureRecognizer隐藏了超级视图的全屏图像。

@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()
}