gpt4 book ai didi

swift - 转到另一个 ImageView 以获取全尺寸图像

转载 作者:行者123 更新时间:2023-11-30 10:12:59 24 4
gpt4 key购买 nike

我有一个照片 View Controller ,可以在表格 View 中显示图像。我正在寻求帮助来连接到 fullsizeimageview ImageView Controller 。我不知道该怎么做。

import UIKit
import Parse
import ParseUI

class PhotosViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let arrSubCatData: NSMutableArray = [];

@IBOutlet weak var tblPhotos: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationItem.title = "Photos"

var tabBar : TabBarController = self.tabBarController as! TabBarController
println(tabBar.parkInfo)


self.tblPhotos.tableFooterView = UIView()

var query = PFQuery(className: "MultiImages")

query.whereKey("placeId", equalTo:tabBar.parkInfo?.valueForKey("objectId") as! String)

query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
self.arrSubCatData.addObjectsFromArray(objects!)
self.tblPhotos?.reloadData();
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("tableViewCell") as! UITableViewCell

if let file:PFFile = (self.arrSubCatData.objectAtIndex(indexPath.row).valueForKey("image") as? PFFile) {
file.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
var imgView : UIImageView = cell.contentView.viewWithTag(100) as! UIImageView
imgView.image = UIImage(data:imageData)
}
}
}
}
return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("segueToInfo", sender: indexPath.row)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrSubCatData.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

/*override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showfullsizeimage" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let detailViewController = segue.destinationViewController as! PhotoFullSizeController
detailViewController.detailItem = objects[indexPath.row]
}
}
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

我新创建的全尺寸图像 Controller 是这样的,带有segue标识符“fullsizeimageview”

import UIKit

class PhotoFullSizeController: UIViewController {

@IBOutlet weak var fullImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

首先,您应该将 TapGesture 添加到 imgView 中。

您的cellForRowIndexPath现在将如下:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("tableViewCell") as! UITableViewCell

if let file:PFFile = (self.arrSubCatData.objectAtIndex(indexPath.row).valueForKey("image") as? PFFile) {
file.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
var imgView : UIImageView = cell.contentView.viewWithTag(100) as! UIImageView
//Add Tap Gesture to the imgView
imgView.userInteractionEnabled = true;
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped"))
imgView.addGestureRecognizer(tapRecognizer)
imgView.image = UIImage(data:imageData)
}
}
}
}
return cell
}

现在为 imageTapped 编写一个处理程序

 //When image is tapped
func imageTapped()
{
println("image tapped")
self.performSegueWithIdentifier("fullsizeimageview", sender: self)
}

并且prepareForSegue将如下所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if(segue.identifier == "fullsizeimageview")
{
let destVC = (segue.destinationViewController as? PhotoFullSizeController)!
let indexPath = self.tblPhotos.indexPathForSelectedRow() //IndexPath for selected table row
if let file:PFFile = (self.arrSubCatData.objectAtIndex(indexPath!.row).valueForKey("image") as? PFFile) {
destVC.imageFile = file

}
}
}

在您的PhotoFullSizeController Controller 中

class PhotoFullSizeController: UIViewController {

@IBOutlet weak var fullImageView: UIImageView!
var imageFile: PFFile!
override func viewDidLoad() {
super.viewDidLoad()
// Load image

self.imageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
//Set image in imageview
fullImageView.image = UIImage(data:imageData)
}
}
}

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

如果有任何问题请告诉我。希望这会有所帮助!

关于swift - 转到另一个 ImageView 以获取全尺寸图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com