用于加载图像的iOS Swift技巧更快

时间:2017-05-08 23:30:24

标签: ios swift image png

我的PNG图像大小从2MB到12MB不等。我通过viewDidLoad中的以下代码使用SDWebImage将其中一个图像加载到imageView中:

function insertTicketSubmission($title, $subject, $question)
{
    global $database;
    global $error;

    $sth = $database->prepare("INSERT INTO `tickets` (`TicketId`, `PlayerId`, `Title`, `Subject`, `Question`, `Date`, `Status`) VALUES (:TicketID, :PlayerID, :Title, :Subject, :Question, GETDATE(), Pending)");
    $sth->bindParam(":TicketID", generateRandomString());
    $sth->bindParam(":PlayerID", $_SESSION["userId"]);
    $sth->bindParam(":Title", $title);
    $sth->bindParam(":Subject", $subject);
    $sth->bindParam(":Question", $question);
    if($sth->execute())
    {
        $error = sendError("success", "Your Ticket has been submitted.");
    }
}

将此图像加载到视图上可能需要17秒。我在这里做错了吗?我只是在我的视图中加载其中一个图像,所以我认为这不会花费这么长时间。我还能做些什么来加快图片加载速度?

是否只能创建一个较小的图像来加载?

3 个答案:

答案 0 :(得分:0)

压缩图像并加载它..如果您通过网络访问它,请确保已启用缓存以加快加载

答案 1 :(得分:0)

我们在后端有一个名为intervention的东西。它为我们提供了一个URL,我们可以通过在URL

之后提供宽度和高度来缩放图像
  

guard let url = imageURL else {return}

     

var smallURL = url +“/”+“(20)”+“/”+“(20)”

所以,我在这里做的是首先加载尺寸为(20x20)的图像并将其显示为模糊图像,当加载实际尺寸的图像时,我将其替换为原始图像。

这样一来,它是一个很好的用户体验,因为用户无需等待图片加载,而是会产生图像正在加载的印象。

所有这些都是在图像视图的扩展中完成的,因此,您可以直接将其称为:

  

imageView.setImage(imageURL:“https://i.stack.imgur.com/GsDIl.jpg”)

extension UIImageView {
  func setImage(imageURL : String?,size : CGFloat = ScreenSize.width ){


    guard let url = imageURL else { return }
        var originalURL = url
        var smallURL = url + "/" + "\(20)" + "/" + "\(20)"

    let actualImageUrl =  URL(string: name)
    let smallURL = URL(string: smallName)
    self.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)



    self.yy_setImage(with: smallURL, placeholder: nil, options: .showNetworkActivity) { (image, _, _, _, _) in

        guard let _ = image else { return }

        let blurredImg = image?.applyBlurWithRadius(0.5, tintColor: UIColor.black.withAlphaComponent(0.4), saturationDeltaFactor: 1.0)
        self.image = blurredImg
        self.yy_setImage(with: actualImageUrl, placeholder: blurredImg, options: .progressiveBlur, completion: { [weak self] (image, url, type, _, _) in
            self?.image = image
        })
    }
  }
}

我没有使用SDWebImage,而是使用了YYWebImage,它提供了更多功能,而且它基于SDWebImage。

希望它能帮助!!

答案 2 :(得分:0)

其他一些建议,如缓存和模糊图像预览都很好,但我建议,此外,您首先考虑更好地优化图像。

查看我对通过缩小图片尺寸来减少应用文件大小的相关问题的回答,

Reducing iOS App file size

PNG文件是iOS的首选图像格式,但您也可以使用JPEG在加载时间内受到轻微影响,但文件缩减的增益通常超过一个数量级或更多。基本上,如果图像包含像照片一样的连续色调的大区域,那么JPEG可能更好。你可以在Photoshop中玩,但7美元(14日发售)Lossless Photo Squeezer可以毫不费力地完成任务。

即使PNG你可以通过降低位深度来节省大小。这就是Loss Photo Squeezer的工作原理下降到8位。如果你不透明,你可以删除alpha通道,或者如果你需要限制,你可以使用photoshop中的1位alpha通道。如果你想要硬核,可以使用带有8位alpha通道的8位JPEG。参见

http://calendar.perfplanet.com/2010/png-that-works/

对于某些图像,您也可以使用低于8的位深度进行播放。

如果你想真正考虑使用WebP。有一些开源iOS实现,例如https://github.com/seanooi/iOS-WebP

您可能还想查看解决方案。视网膜图像非常适合细节,如文字,但我经常使用1倍分辨率的图像甚至在视网膜显示器上拍照。这很好,因为如果您需要分辨率来阅读文本,那么PNG很可能会很好地压缩。

如果您的图像具有适合JPEG的连续色调和某些区域(例如更适合PNG的文本),您可以发疯并将图像分成JPEG和带有alpha的8位PNG通道并在下载后合并两个图像。

相关问题