現在、Phonegap/Cordova と jQuerymobile を使用して iOS 用のアプリを構築しています。アイデアは、カメラで写真を撮り、キャプチャした画像を将来の使用のために保存することです。パス/ファイル名をローカル データベースに保存し、画像ファイルを iPhone の永続的な場所に移動したいと考えています。
誰かが私に例を提供してもらえますか?
現在、Phonegap/Cordova と jQuerymobile を使用して iOS 用のアプリを構築しています。アイデアは、カメラで写真を撮り、キャプチャした画像を将来の使用のために保存することです。パス/ファイル名をローカル データベースに保存し、画像ファイルを iPhone の永続的な場所に移動したいと考えています。
誰かが私に例を提供してもらえますか?
わかりました、これが解決策です。
Htmlファイルで
カメラで撮影した写真を表示するためのイメージタグがあります:
写真を撮るための機能を実行するボタンがあります:写真のキャプチャ
写真をキャプチャする機能は次のとおりです(写真が撮影されると、「smallImage」IDのscrに写真のパスが入力されます)
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
}
//Callback function when the picture has been successfully taken
function onPhotoDataSuccess(imageData) {
// Get image handle
var smallImage = document.getElementById('smallImage');
// Unhide image elements
smallImage.style.display = 'block';
smallImage.src = imageData;
}
//Callback function when the picture has not been successfully taken
function onFail(message) {
alert('Failed to load picture because: ' + message);
}
次に、画像を永続的なフォルダーに移動し、リンクをデータベースに保存します。
function movePic(file){
window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError);
}
//Callback function when the file system uri has been resolved
function resolveOnSuccess(entry){
var d = new Date();
var n = d.getTime();
//new file name
var newFileName = n + ".jpg";
var myFolderApp = "EasyPacking";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
//The folder is created if doesn't exist
fileSys.root.getDirectory( myFolderApp,
{create:true, exclusive: false},
function(directory) {
entry.moveTo(directory, newFileName, successMove, resOnError);
},
resOnError);
},
resOnError);
}
//Callback function when the file has been moved successfully - inserting the complete path
function successMove(entry) {
//I do my insert with "entry.fullPath" as for the path
}
function resOnError(error) {
alert(error.code);
}
ファイルを表示するためにデータベースに保存しました。画像 src を含む行の前に「file://」を追加しました
この助けを願っています。J.
PS : - Simon Mac Donald (http://hi.im/simonmacdonald) の googledocs への投稿に感謝します。
ジェロームの答えは魅力のように機能します..ファイルを使用したいときに人々に指摘する唯一のことは、entry.fullPathを使用しないでください。これは問題を引き起こすことがあるため、entry.toURL()を使用してください。パスに「file://」を追加する必要がなく、SD カード ストレージなどの問題を回避できます。
3つのステップがあります:
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *filename = @"mypic.png";
NSString *fullpath = [docDir stringByAppendingPathComponent:filename];
[UIImagePNGRepresentation(image) writeToFile:fullpath atomically:YES];