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
When calling the method specified in the selector from the iOS side, the parameters you want to pass are determined by which iOS features you use.
For UIGestureRecognizer
, it must be one of the following signatures:
@IBAction func myActionMethod()
@ IBAction func myActionMethod(_sender:UIGestureRecognizer)
@IBAction
is just a document description, so you can quickly ignore it, and the point is that you can take up to one argument, one of which represents UIGestureRecognizer
.
Even if arguments are added in the method declaration, iOS ignores them at all and passes the first argument an instance of UIGestureRecognizer
(UITapGestureRecognizer
in your case).Your method forcibly interprets it as MyUIImageView
and calls a property getter for MyUIImageView
, so it's safe to crash immediately rather than get a proper value back.
Declare the exact arguments for the method called by the selector.
@objc funcimageViewTapped(_gestureRecognizer:UITapGestureRecognizer){
// Use the `view` property to retrieve the view to which you want to set the UIGestureRecognizer
// I intentionally use `!` or `as!` to inform you of the crash if the UITapGestureRecognizer is misconfigured.
let myImageView=gestureRecognizer.view!as!MyUIImageView
print(myImageView.x)
}
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
581 PHP ssh2_scp_send fails to send files as intended
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.