【问题标题】:UIImage slideshow from array数组中的 UIImage 幻灯片
【发布时间】:2016-04-27 20:53:50
【问题描述】:

使用这个 Github 扩展从 URL 设置图像:https://github.com/Haneke/HanekeSwift

我将图像存储在这样的数组中:

var userFile = [PFFile]()

图像存储为 URL,我将图像设置在 UIImageView 中,如下所示:

imageView.hnk_setImageFromURL(NSURL(string: userFile[indexPath.row].url!)!, placeholder: nil, format: nil, failure: nil, success: nil)

如何在同一个UIImageView 中显示所有图像,但要查看下一张照片,我必须点按UIImageView

【问题讨论】:

  • 您希望它们并排还是在点击时一个接一个地切换?
  • @EugeneZhenyaGordin - 首先我想显示第一张图片,然后切换到下一个点击。不完全像“幻灯片”,但是当我点击UIImageView 时,图像会变为下一张。

标签: ios swift uiimageview uiimage


【解决方案1】:

这是 Obj-c 示例如何做到这一点。在swift中它非常相似。

为您的图像视图添加手势(点击)识别器:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self.yourImageView addGestureRecognizer:tap];

然后点击:

- (void)tapped:(UIGestureRecognizer*)sender
{
    UIImageView* yourImageView = (UIImageView*)[sender view]; // touch is detected on the imageView

    yourImageView.image = // your image from url
}

斯威夫特:

// setup gesture recognizer somewhere in the viewDidLoad:

let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))

 yourImageView.addGestureRecognizer(tap)

// this function will be called on tap
func handleTap(gestureRecognizer: UIGestureRecognizer)
{
    let tappedView = gestureRecognizer.view as! UIImageView
    tappedView.image = // add the image needed here
}

【讨论】:

  • 我在“//在这里添加需要的图片”里面放什么?
  • 无论您对图像使用什么,在您的情况下:hnk_setImageFromURL(NSURL(string: userFile[indexPath.row].url!)!,占位符:nil,格式:nil,失败:nil,成功: 无),对吧?您是从 URL 获取的
  • 是的,但我收到此错误:Use of unresolved identifier "indexPath"
  • 确保创建一种在您点击视图时获取下一张图片的机制,这样您就不会一直加载同一张图片。在不知道您如何获取图像的内部实现的情况下,我无法为您提供帮助。
  • 我正在从 Parse 获取图像
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 2015-04-21
  • 2021-02-20
  • 1970-01-01
  • 2015-05-12
  • 2017-12-26
相关资源
最近更新 更多