Sample app taking pictures with camera and adding them in a ScrollView crashes on iPhone 4 after 7-8 images are added. The log shows a "Memory warning" message. No issues found with iPhone 5.
var damageImages = [];
var win = Ti.UI.createWindow({
backgroundColor : '#fff'
});
var imageView = Ti.UI.createView({
layout : 'horizontal',
width : Ti.UI.FILL,
height : Ti.UI.SIZE
});
var successCallBack = function(e) {
if (e.mediaType != Ti.Media.MEDIA_TYPE_PHOTO) {
takePhotoBtn.enabled = true;
pickPhotoBtn.enabled = true;
return false;
}
var filename = (new Date().getTime()) + ".jpg";
var newImageWrapperView = Ti.UI.createView({
backgroundColor : '#000',
left : '8dp',
top : '10dp',
height : '96dp',
width : '96dp',
isImage : true
});
newImageWrapperView.id = filename;
var newImageView = Ti.UI.createImageView({
isImage : true,
zIndex : 10
});
newImageView.opacity = 0.5;
newImageWrapperView.add(newImageView);
var bgImage = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
newImageView.image = bgImage.getNativePath();
//I need this for let use..
damageImages.push({
"id" : bgImage.getNativePath(),
"name" : filename,
"description" : ""
});
//resizing code also removed
/*
var imageModify = Ti.UI.createImageView({
image : e.media
});
var width = imageModify.toImage().width
var height = imageModify.toImage().height
imageModify = null;
var scaledImage = require('lib/utils').resizeKeepAspectRatioNewWidth(e.media, width, height, Config.imageMaxWidth);
*/
bgImage.write(e.media);
//scaledImage = null;
bgImage = null;
imageView.add(newImageWrapperView);
takePhotoBtn.enabled = true;
pickPhotoBtn.enabled = true;
};
var errorCallBack = function(e) {
takePhotoBtn.enabled = true;
pickPhotoBtn.enabled = true;
alert(L('erroronselectimage', 'Error while selecting image'));
};
var cancelCallBack = function(e) {
takePhotoBtn.enabled = true;
pickPhotoBtn.enabled = true;
};
var takePhotoBtn = Titanium.UI.createButton({
style : Titanium.UI.iPhone.SystemButtonStyle.BORDERED,
title : 'Take Photo'
});
takePhotoBtn.addEventListener('click', function(e) {
Ti.Media.showCamera({
success : successCallBack,
error : errorCallBack,
cancel : cancelCallBack,
allowEditing : false,
saveToPhotoGallery : true,
mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
});
takePhotoBtn.enabled = false;
pickPhotoBtn.enabled = false;
});
var pickPhotoBtn = Titanium.UI.createButton({
style : Titanium.UI.iPhone.SystemButtonStyle.BORDERED,
title : 'Pick Photo'
});
pickPhotoBtn.addEventListener('click', function(e) {
Ti.Media.openPhotoGallery({
success : successCallBack,
error : errorCallBack,
cancel : cancelCallBack,
allowEditing : false,
mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
});
takePhotoBtn.enabled = false;
pickPhotoBtn.enabled = false;
});
var flexSpace = Titanium.UI.createButton({systemButton : Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE});
var toolbar = Ti.UI.iOS.createToolbar({
borderTop : true,
borderBottom : false,
height : 45,
zIndex : '100',
bottom : '0dp'
});
toolbar.items = [flexSpace, takePhotoBtn, pickPhotoBtn, flexSpace];
var scrollView = Ti.UI.createScrollView({
layout : 'vertical',
top : 0,
bottom : 45
});
scrollView.add(imageView);
win.add(scrollView);
win.add(toolbar);
win.open();
During tests, devices even crashed entirely (e.g. reboot).
Hi, I tested with iPhone 4s and app is crashing most of the cases after taking 6-8 images using camera. I don't think this is problem with adding to Scrollview. Because app is crashing when we open the camera using
showCamera
only. Can you please make this high Priority. Thank you.HI, Verified The issue with iPhone 5 also and Crashes on that also but number of images taken are quite high approx 30 to 35. **Device Crash Log**
Hi, This particular line is interesting here, in the above device log. **conceptTest <33514e224eeb36d182595067e925de72> 159731 160082 [per-process-limit] (frontmost) (resume)**
The way the code is written the imageView loads the entire image into memory. The RGB data of the image occupies about 30 MB in VM (Use VM tracker on Instruments) So once you have taken about 8 photographs the app gets killed due to lack of memory. Given that the imageView is going to be at most 96x96 in size, it would be better to resize the image (keeping aspect ratio intact) and then use the resized image in the thumbnail view. The original image can be loaded later when needed. Code is attached below to show loading of scaled images. App no longer crashes. Scaling Logic
Full Code
Closing ticket as "Won't Fix".