2014-04-04 222 views

回答

0

請嘗試使用iOS Canvas2ImagePlugin的自定義插件。這將從手機圖像傳遞到本地(目標C)。要保存圖像到相冊

在iOS項目中添加2個文件SaveToPhotoAlbum.h和SaveToPhotoAlbum.m

SaveToPhotoAlbum.h

#import <Foundation/Foundation.h> 
#import <Cordova/CDVPlugin.h> 


@interface SaveToPhotoAlbum : CDVPlugin { 
} 

- (void) saveToPhotoAlbum:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; 

@end 

SaveToPhotoAlbum.m

#import "SaveToPhotoAlbum.h" 

@implementation SaveToPhotoAlbum 

- (void) saveToPhotoAlbum:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { 
    NSString* callbackId = [arguments pop]; 
    NSString* path = [arguments objectAtIndex:0]; 

    path = [(NSString *)path stringByReplacingOccurrencesOfString:@"+" withString:@" "]; 
    path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    path = [path stringByReplacingOccurrencesOfString:@"file://" withString:@""]; 

    UIImage *image = [[[UIImage alloc] initWithContentsOfFile:path] autorelease]; 

    //Now it will do this for each photo in the array 
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 

    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; 
    [self writeJavascript: [pluginResult toSuccessCallbackString:callbackId]]; 
} 

@end 

saveToPhotoAlbum.js

(function(cordova) { 

/** 
* Constructor 
*/ 
function SaveToPhotoAlbum() {} 


SaveToPhotoAlbum.prototype.saveToPhotoAlbum = function(successCallback, failCallback, uri) { 
    console.log('SaveToPhotoAlbum.js: saveToPhotoAlbum: uri:' + uri); 
    if (!uri) { 
     if (typeof failCallback === 'function') { 
      failCallback('uri not provided'); 
     } 
     return; 
    } 

    cordova.exec(
     successCallback, 
     failCallback, 
     "SaveToPhotoAlbum", 
     "saveToPhotoAlbum", 
     [uri] 
    ); 
}; 

/** 
* Install function 
*/ 
SaveToPhotoAlbum.install = function() { 
    if (!window.plugins) { 
     window.plugins = {}; 
    } 
    if (!window.plugins.SaveToPhotoAlbum) { 
     window.plugins.SaveToPhotoAlbum = new SaveToPhotoAlbum(); 
    } 
}; 

/** 
* Add to Cordova constructor 
*/ 
if (cordova && cordova.addConstructor) { 
    cordova.addConstructor(SaveToPhotoAlbum.install); 
} else { 
    console.log("SaveToPhotoAlbum Cordova Plugin could not be installed."); 
    return null; 
} 

})(window.PhoneGap || window.Cordova || window.cordova); 

,並在你的HTML文件作爲來電:

var imageSource = $('#widgetScreen_widgetContainer [name="mobileimage_66"]')[0].src; 
      window.plugins.SaveToPhotoAlbum.saveToPhotoAlbum(photoSuccess, photoFail, imageSource); 
+0

我用什麼,如果我沒有畫布,但仍然有一個彩信圖像保存? – confile

+0

canvas2image是插件的名稱,但它將本地或遠程圖像保存到iOS保存的相冊中 – Vinodh

+0

如何使用此插件保存使用var image = new Image()創建的圖像。 ? – confile