質問編集履歴

4

訂正とdeleteなし版を追記

2018/12/18 07:52

投稿

flc
flc

スコア13

test CHANGED
File without changes
test CHANGED
@@ -188,98 +188,180 @@
188
188
 
189
189
 
190
190
 
191
+ ```JavaScript
192
+
193
+ let tags = [];
194
+
195
+
196
+
197
+ const blobbing = (index) => {
198
+
199
+
200
+
201
+ const serial = `000${index}`.slice(-4);
202
+
203
+ let img = new Image(); //あとでdeleteしてメモリから解放するので、constではなくlet
204
+
205
+
206
+
207
+ //ローカル画像を読み込むため、Chromeの起動オプションにあらかじめ--allow-file-access-from-filesをつけてあります
208
+
209
+ img.crossOrigin = "Anonymous";
210
+
211
+ img.src = `file:///C:/新しいフォルダー/${serial}.jpg`; //元のサイズは1920 * 1080px
212
+
213
+
214
+
215
+ img.onload = () => {
216
+
217
+
218
+
219
+ ctx.drawImage( img, 0, 0, this.width, this.height, 0, 0, w, h );
220
+
221
+ delete img; //ここで元画像をメモリから解放(すぐに解放されるわけではなく時間差がある?)
222
+
223
+
224
+
225
+ canvas.toBlob( (blob) => {
226
+
227
+
228
+
229
+ const url = URL.createObjectURL( blob );
230
+
231
+ tags[index] = `<img src="${url}" title="${serial}" style="width: ${w}px; height: ${h}px;">`;
232
+
233
+ index++;
234
+
235
+
236
+
237
+ if( index === max ) {
238
+
239
+
240
+
241
+ $('.images').append( tags.join() );
242
+
243
+ }
244
+
245
+ else {
246
+
247
+
248
+
249
+ blobbing( index ); //自分自身を呼び出してループ
250
+
251
+ }
252
+
253
+
254
+
255
+ });
256
+
257
+
258
+
259
+ }
260
+
261
+
262
+
263
+
264
+
265
+ }
266
+
267
+
268
+
269
+ blobbing( 0 ); //ループ開始
270
+
271
+ ```
272
+
273
+
274
+
275
+ あるいはdeleteを使わなくても、単にlet img = new Image(); で最初に作ったオブジェクトをそのままずっと使いまわしてもいいのかもしれません(そのほうがよかったりするのかはわかりませんが)
276
+
277
+
278
+
279
+ ```JavaScript
280
+
281
+ let tags = [];
282
+
283
+ let img = new Image();
284
+
285
+
286
+
287
+ const blobbing = (index) => {
288
+
289
+
290
+
291
+ const serial = `000${index}`.slice(-4);
292
+
293
+
294
+
295
+ //ローカル画像を読み込むため、Chromeの起動オプションにあらかじめ--allow-file-access-from-filesをつけてあります
296
+
297
+ img.crossOrigin = "Anonymous";
298
+
299
+ img.src = `file:///C:/新しいフォルダー/${serial}.jpg`; //元のサイズは1920 * 1080px
300
+
301
+
302
+
303
+ img.onload = () => {
304
+
305
+
306
+
307
+ ctx.drawImage( img, 0, 0, this.width, this.height, 0, 0, w, h );
308
+
309
+
310
+
311
+ canvas.toBlob( (blob) => {
312
+
313
+
314
+
315
+ const url = URL.createObjectURL( blob );
316
+
317
+ tags[index] = `<img src="${url}" title="${serial}" style="width: ${w}px; height: ${h}px;">`;
318
+
319
+ index++;
320
+
321
+
322
+
323
+ if( index === max ) {
324
+
325
+
326
+
327
+ $('.images').append( tags.join() );
328
+
329
+ }
330
+
331
+ else {
332
+
333
+
334
+
335
+ blobbing( index ); //自分自身を呼び出してループ
336
+
337
+ }
338
+
339
+
340
+
341
+ }
342
+
343
+
344
+
345
+ blobbing( 0 ); //ループ開始
346
+
347
+ ```
348
+
349
+
350
+
351
+ 2000枚近く読み込めるようにはなったものの、
352
+
353
+
354
+
355
+ > net::ERR_INSUFFICIENT_RESOURCES 200 (OK)
356
+
357
+
358
+
359
+ というのが出ますね… やはりリソースがあと少し足りないようで
360
+
361
+
362
+
191
363
  (ここまでしてBlobを使って画像を縮小する必要があるのか、そもそも複数の方からのご意見があったようにJavaScript以外の手段で縮小画像を作っておくべきなのかという話にもなってきますが)
192
364
 
193
365
 
194
366
 
195
- ```JavaScript
196
-
197
- let tags = [];
198
-
199
-
200
-
201
- const blobbing = (index) => {
202
-
203
-
204
-
205
- const serial = `000${index}`.slice(-4);
206
-
207
- let img = new Image(); //あとでdeleteしてメモリから解放するので、constではなくlet
208
-
209
-
210
-
211
- //ローカル画像を読み込むため、Chromeの起動オプションにあらかじめ--allow-file-access-from-filesをつけてあります
212
-
213
- img.crossOrigin = "Anonymous";
214
-
215
- img.src = `file:///C:/新しいフォルダー/${serial}.jpg`; //元のサイズは1920 * 1080px
216
-
217
-
218
-
219
- img.onload = () => {
220
-
221
-
222
-
223
- ctx.drawImage( img, 0, 0, this.width, this.height, 0, 0, w, h );
224
-
225
-
226
-
227
- canvas.toBlob( (blob) => {
228
-
229
-
230
-
231
- const url = URL.createObjectURL( blob );
232
-
233
-
234
-
235
- newImg.src = url;
236
-
237
- newImg.title = serial;
238
-
239
- document.body.appendChild( newImg );
240
-
241
-
242
-
243
- });
244
-
245
-
246
-
247
- }
248
-
249
-
250
-
251
- delete img; //ここで元画像をメモリから解放(すぐに解放されるわけではなく時間差がある?)
252
-
253
-
254
-
255
- index++;
256
-
257
-
258
-
259
- if( index === max ) {
260
-
261
-
262
-
263
- $('.images').append( tags.join() );
264
-
265
- }
266
-
267
- else {
268
-
269
-
270
-
271
- blobbing( index ); //自分自身を呼び出してループ
272
-
273
- }
274
-
275
- }
276
-
277
-
278
-
279
- blobbing( 0 ); //ループ開始
280
-
281
- ```
282
-
283
-
284
-
285
367
  引き続きご意見をお待ちしております。

3

再帰的?に

2018/12/18 07:52

投稿

flc
flc

スコア13

test CHANGED
File without changes
test CHANGED
@@ -163,3 +163,123 @@
163
163
 
164
164
 
165
165
  (もしかしたらそもそもHTML + JavaScriptで作ることを諦めたほうがよかったりするのかもしれませんが、知識不足でして…)
166
+
167
+
168
+
169
+ ### 追記 再帰的?にしてみたもの
170
+
171
+
172
+
173
+ for文をやめて以下に書き換えてみると、toBlob()する前に生成していた元画像ぶんの余計なメモリ使用が抑えられたようです
174
+
175
+ (それでも3000枚は表示できませんが)
176
+
177
+
178
+
179
+ 都度delete演算子で画像オブジェクトのプロパティを削除することで、ガベージコレクションがメモリを解放してくれるのを期待してこのような形ですが(そういうことを意識するのは初めてなので合っているかわかりません)
180
+
181
+
182
+
183
+ そのため、すぐにメモリが解放されるわけではなく時間差があるようで、Chromeに内属されたタスクマネージャで見ていると一旦メモリがある程度増加してから下がる… という挙動を見せています
184
+
185
+
186
+
187
+ なのでだんだんblobによるメモリ使用量が増えてくる後半、ヒヤヒヤしながら見守るのは変わらず…(うちのPCでは600MBほど使うとChromeが落ちる?ようです)
188
+
189
+
190
+
191
+ (ここまでしてBlobを使って画像を縮小する必要があるのか、そもそも複数の方からのご意見があったようにJavaScript以外の手段で縮小画像を作っておくべきなのかという話にもなってきますが)
192
+
193
+
194
+
195
+ ```JavaScript
196
+
197
+ let tags = [];
198
+
199
+
200
+
201
+ const blobbing = (index) => {
202
+
203
+
204
+
205
+ const serial = `000${index}`.slice(-4);
206
+
207
+ let img = new Image(); //あとでdeleteしてメモリから解放するので、constではなくlet
208
+
209
+
210
+
211
+ //ローカル画像を読み込むため、Chromeの起動オプションにあらかじめ--allow-file-access-from-filesをつけてあります
212
+
213
+ img.crossOrigin = "Anonymous";
214
+
215
+ img.src = `file:///C:/新しいフォルダー/${serial}.jpg`; //元のサイズは1920 * 1080px
216
+
217
+
218
+
219
+ img.onload = () => {
220
+
221
+
222
+
223
+ ctx.drawImage( img, 0, 0, this.width, this.height, 0, 0, w, h );
224
+
225
+
226
+
227
+ canvas.toBlob( (blob) => {
228
+
229
+
230
+
231
+ const url = URL.createObjectURL( blob );
232
+
233
+
234
+
235
+ newImg.src = url;
236
+
237
+ newImg.title = serial;
238
+
239
+ document.body.appendChild( newImg );
240
+
241
+
242
+
243
+ });
244
+
245
+
246
+
247
+ }
248
+
249
+
250
+
251
+ delete img; //ここで元画像をメモリから解放(すぐに解放されるわけではなく時間差がある?)
252
+
253
+
254
+
255
+ index++;
256
+
257
+
258
+
259
+ if( index === max ) {
260
+
261
+
262
+
263
+ $('.images').append( tags.join() );
264
+
265
+ }
266
+
267
+ else {
268
+
269
+
270
+
271
+ blobbing( index ); //自分自身を呼び出してループ
272
+
273
+ }
274
+
275
+ }
276
+
277
+
278
+
279
+ blobbing( 0 ); //ループ開始
280
+
281
+ ```
282
+
283
+
284
+
285
+ 引き続きご意見をお待ちしております。

2

サイズ・容量など追記・訂正

2018/12/18 07:30

投稿

flc
flc

スコア13

test CHANGED
File without changes
test CHANGED
@@ -8,13 +8,13 @@
8
8
 
9
9
  - ファイル名 … 「0000.jpg」~「9999.jpg」(実際は3000枚程度)
10
10
 
11
- - 大きさ … 1920 * 1080px
11
+ - 大きさ … 1280 * 720px
12
12
 
13
13
  - 容量 … 200KB前後
14
14
 
15
15
 
16
16
 
17
- となっています。
17
+ となっています。フォルダーの総容量はおよそ1GB台、使用PCのRAMは4GBです。
18
18
 
19
19
 
20
20
 
@@ -50,9 +50,9 @@
50
50
 
51
51
 
52
52
 
53
- const WIDTH = 1920;
53
+ const WIDTH = 1280;
54
54
 
55
- const HEIGHT = 1080;
55
+ const HEIGHT = 720;
56
56
 
57
57
 
58
58
 
@@ -60,9 +60,9 @@
60
60
 
61
61
 
62
62
 
63
- const w = WIDTH / divide; //1920 ÷ 2 = 960
63
+ const w = WIDTH / divide; //1280 ÷ 2 = 640
64
64
 
65
- const h = HEIGHT / divide; //1080 ÷ 2 = 540
65
+ const h = HEIGHT / divide; //720 ÷ 2 = 360
66
66
 
67
67
 
68
68
 

1

alt → title

2018/12/18 05:30

投稿

flc
flc

スコア13

test CHANGED
File without changes
test CHANGED
@@ -124,7 +124,7 @@
124
124
 
125
125
  newImg.src = url;
126
126
 
127
- newImg.alt = serial;
127
+ newImg.title = serial;
128
128
 
129
129
  document.body.appendChild( newImg );
130
130