Alex Khanenya Alex Khanenya - 1 month ago 10
iOS Question

Camera doesn't work after compilation

I use Ionic Framework.

Camera works perfectly in Ionic View. But it doesn't work after compilitaion on iOS.

Please, help to find out what is the problem.

Camera code in services.js:

app.factory('FileService', function() {
var images;
var IMAGE_STORAGE_KEY = 'dav-images';

function getImages() {
var img = window.localStorage.getItem(IMAGE_STORAGE_KEY);
if (img) {
images = JSON.parse(img);
} else {
images = [];
}
return images;
};

function addImage(img) {
images.push(img);
window.localStorage.setItem(IMAGE_STORAGE_KEY, JSON.stringify(images));
};

return {
storeImage: addImage,
images: getImages
}
});

app.factory('ImageService', function($cordovaCamera, FileService, $q, $cordovaFile) {

function optionsForType(type) {
var source;
switch (type) {
case 0:
source = Camera.PictureSourceType.CAMERA;
break;
case 1:
source = Camera.PictureSourceType.PHOTOLIBRARY;
break;
}
return {
quality: 90,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: source,
allowEdit: false,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true
};
}

function saveMedia(type) {
return $q(function(resolve, reject) {
var options = optionsForType(type);

$cordovaCamera.getPicture(options).then(function(imageBase64) {
FileService.storeImage(imageBase64);
});
})
}
return {
handleMediaDialog: saveMedia
}
});


Camera code in controllers.js:

app.controller('CameraController', function($scope, $cordovaDevice, $cordovaFile, $ionicPlatform, $cordovaEmailComposer, $ionicActionSheet, ImageService, FileService) {

$ionicPlatform.ready(function() {
$scope.images = FileService.images();
$scope.$apply();
});

$scope.addMedia = function() {
$scope.hideSheet = $ionicActionSheet.show({
buttons: [
{ text: 'Take photo' },
{ text: 'Photo from library' }
],
titleText: 'Add images',
cancelText: 'Cancel',
buttonClicked: function(index) {
$scope.addImage(index);
}
});
}

$scope.addImage = function(type) {
$scope.hideSheet();
ImageService.handleMediaDialog(type).then(function() {
$scope.$apply();
});
}

$scope.sendEmail = function() {
if ($scope.images != null && $scope.images.length > 0) {
var mailImages = [];
var savedImages = $scope.images;
for (var i = 0; i < savedImages.length; i++) {
mailImages.push('base64:attachment'+i+'.jpg//' + savedImages[i]);
}
$scope.openMailComposer(mailImages);
}
}

$scope.openMailComposer = function(attachments) {
var bodyText = '<html><h2>My Images</h2></html>';
var email = {
to: '',
attachments: attachments,
subject: 'Devdactic Images',
body: bodyText,
isHtml: true
};

$cordovaEmailComposer.open(email, function(){
console.log('email view dismissed');
}, this);
}
});

Answer

To resolve this problem you need to compile an app with XCode. After XCode compilation camera works correctly.

Probably, the problem with camera was caused by Ionic compilation.