[英]How to save image to Photo gallery using JavaScript/Obj-C on iPad?
我目前正在 iPad 上開發一個基於 JavaScript 的小型繪圖應用程序。 我只是像這樣將它包裹在 UIWebView 中。 現在問題是我不知道如何從應用程序中保存圖像,以便將其存儲在 iPad 照片上。
有沒有一些簡單的方法可以將 JavaScript 代碼與 Objective-C 代碼連接起來? 我想我可以從 JS 端傳遞一個包含 PNG 圖像數據的字符串,然后使用 Objective-C 以某種方式將其保存到照片庫中。
是的,有一種方法可以將 javascript 從 UIWebView 橋接到 Obj-C 代碼。
設置您的 UIWebView 委托,並在您的委托中粘貼以下方法
//
// Map links starting with file://
// ending with #action
// with the action of the controller if it exists.
//
// Open other links in Safari.
- (BOOL)webView: (UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)request navigationType: (UIWebViewNavigationType)navigationType {
NSString *fragment, *scheme;
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[webView stopLoading];
fragment = [[request URL] fragment];
scheme = [[request URL] scheme];
if ([scheme isEqualToString: @"file"] && [self respondsToSelector: NSSelectorFromString(fragment)]) {
[self performSelector: NSSelectorFromString(fragment)];
return NO;
}
[[UIApplication sharedApplication] openURL: [request URL]];
}
return YES;
}
然后你可以編寫處理請求的 Obj-C 方法。 例如。 在您的 webview 中,您可能有一個帶有鏈接標簽的按鈕,例如
<a href="file://myGreatApp.com/saveImage">Click Me to Save Image </a>
方案是“文件”,片段是“saveImage”。 您現在可以編寫一個 Obj-C 方法
-(void)saveImage;
每次用戶點擊鏈接時都會調用它。
編輯:如果你想在你的方法中傳遞字符串參數,只需 append 使用 javascript 將它們傳遞給你的片段 url。 所以你可能有
<a href="file://myGreatApp.com/saveImage*asdgo8asdgl35lkjasgd807ll12">Click Me to Save Image </a>
然后在您的 Obj-C 代碼中,在字符“*”上拆分片段。 使用前半部分作為選擇器,后半部分作為參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.