笔记:iPhone 6 Plus with iOS 8.1.2 canvas 图片剪裁问题


 拍摄于 2016-12-11,朋友家的猫,一只刚割了蛋蛋,生无可恋、望眼欲穿、天天躺尸的猫。
拍摄于 2016-12-11,朋友家的猫,一只刚割了蛋蛋,生无可恋、望眼欲穿、天天躺尸的猫。

问题

iPhone 6 Plus 前端上传图片,使用 Canvas 剪裁后 toDataURL() 转换出空 data: 问题。StackOverflow 上的这一个问题几乎是一摸一样的问题:《File Upload shows data with empty strings on Safari and Chrome iOS8.1.2

TL;DR Recently I encountered similar issue as described in question, found this thread, it may has been a low memory issue with iPhone, and not much work around you can do but resize the big size image with canvas element, I use 1200 pixels as the longest width/height for the image to resize then everything works again.

for any feature reference, more specific hardware and software the problem occurred with my test is iPhone 6 Plus and iOS 8.1.2, and the process I tried to do with images is:

  1. using FileReader.onLoad = func with FileReader.readAsDataURL() to read input[type="file"]’s file as base64 string.
  2. assign base64 as new image()’s src
  3. inside image.onload = func draw a new canvas’s 2d context with the same with and height as image above.
  4. check some orientation EXIF info then rotate the canvas.
  5. finally do other staffs with base64 string of new image which generated form canvas.toDataURL().
  6. The empty data: occurs in both between step 1 to step 2 and step 5 randomly depends on the size of image got from `input[type=“file”].

In my case, iOS Safari here seems being okay to assign image data to canvas with same width and height, but not okay to toDataURL for the canvas if it has a very big size. But Safari won’t hang anyway, I can re-tap input[type=“file”] to get next file since the empty data: outputted from previous one.

感谢阅读

你们好, 2018 年初把小站从 Jekyll 迁移到 Hugo 的过程中,删除了评论区放的 Disqus 插件,考虑有二:首先无论评论、还是对笔记内容的进一步讨论,读者们更喜欢通过邮件、或者 Twitter 私信的方式来沟通;其次一年多以来 Disqus 后台能看到几乎都是垃圾留言(spam),所以这里直接贴一下邮件、以及 Twitter 账户 地址。

技术发展迭代很快,所以这些笔记内容也有类似新闻的时效性,不免有过时、或者错误的地方,欢迎指正 ^_^。

BEST
Lien(A.K.A 胡椒)
本站总访问量 本站总访客量 本文总阅读量