What happens when you tap an image with Swift?

Asked 1 years ago, Updated 1 years ago, 98 views

Currently, I am creating an iOS app that retrieves multiple thumbnails from a single video I read and arranges them horizontally.I was able to get thumbnails from the video and arrange them side by side, but when I tap one thumbnail from there, I want to display it in another UIImageView, but I can't do it well.

The current method is to create a class that inherits UIImageView and send the value to the tap event, but random numbers such as 15 digits instead of 0, 1, 2 are displayed.

I don't know why such a value is returned, so I would appreciate it if you could let me know.Another way is fine.

The following are the source code, execution results and reference sites.

Source Code

import UIKit
import AVFoundation
import AssetsLibrary

classViewController:UIViewController, UIGestureRecognizerDelegate{

    @IBOutlet weak var topimage:UIImageView!
    @IBOutlet weak var scroll:UIScrollView!

    let time = 20

    class MyUIImageView:UIImageView {
        let x —Int
        letty —Int
        init(x:Int, y:Int, frame:CGRect){
            self.x = x
            self.y = y
            super.init (frame:frame)
        }
        required init (coder adecoder:NSCoder) {
            fatalError("***init(coder:)has not been implemented")
        }
    }

    override func viewDidLoad(){
        super.viewDidLoad()

        let filePath: URL = URL (fileURLWithPath: "Video Path")
        letasset=AVURLAsset(url:filePath, options:nil)
        let imgGenerator = AVAssetImageGenerator (asset:asset)
        imgGenerator.appliesPreferredTrackTransform=true

        scroll.contentSize=CGSize(width:time*100, height:200)
        scroll.bounce=false
        self.scroll.isScrollEnabled=true
        self.scroll.showsHorizontalScrollIndicator=false

        do{
            for x in 0 ... time-1 {
            // Create a view while changing the position
                letiv: UIImageView= MyUIImageView(
                    x —x,
                    y—1,
                    frame —CGRect (x:CGFloat(x)*100, y:100, width:100, height:150))

                // Creating Thumbnails
                let cgImage=try imgGenerator.copyCGImage(at:CMTimeMake(Int64(seconds/(time-x))), 3),actualTime:nil)
                let thumbnail=UIImage(cgImage:cgImage)

                // Add image to view
                iv.image=thumbnail
                // Tap gesture
                iv.isUserInteractionEnabled=true
                iv.addGestureRecognizer(UITapGestureRecognizer(
                    target —self,
                    action:#selector(imageViewTapped)))

                // Add to screen
                scroll.addSubview(iv)
            }
        }catchlet error {
            print("***Error generating thumbnail:\(error.localizedDescription)")
        }
    }

    @objc funcimageViewTapped(views:MyUIImageView, gestureRecognizer:UITapGestureRecognizer){
        // If I can get x here, I think I can display it if I write the same program as above.
        print(views.x)
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()

    }
}

Run Results

 view at (140434724710272) is tapped // 1st thumbnail
view at(0)is tapped // Second thumbnail
view at (140434743678464) is taped // Third thumbnail
view at (140434724710848) is taped // 4th thumbnail

Reference Site
https://qiita.com/fetaro/items/199e36d71ee9c0015488

swift ios

2022-09-30 21:27

1 Answers

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.