Add new sample - ImageProtocol

This commit is contained in:
Rolf Bjarne Kvinge 2012-04-17 22:47:58 +02:00
Родитель 4045590c65
Коммит 272470b934
16 изменённых файлов: 424 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,70 @@
//
// AppDelegate.cs
//
// Author:
// Rolf Bjarne Kvinge (rolf@xamarin.com)
//
// Copyright 2012, Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace ImageProtocol
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
UIWebView web;
UIViewController controller;
public override bool FinishedLaunching (UIApplication application, NSDictionary options)
{
// Register our custom url protocol
NSUrlProtocol.RegisterClass (new MonoTouch.ObjCRuntime.Class (typeof (ImageProtocol)));
controller = new UIViewController ();
web = new UIWebView (UIScreen.MainScreen.Bounds) {
BackgroundColor = UIColor.White,
ScalesPageToFit = true,
AutoresizingMask = UIViewAutoresizing.All
};
controller.NavigationItem.Title = "Test case";
controller.View.AutosizesSubviews = true;
controller.View.AddSubview (web);
web.LoadRequest (NSUrlRequest.FromUrl (NSUrl.FromFilename (NSBundle.MainBundle.PathForResource ("test", "html"))));
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.MakeKeyAndVisible ();
window.RootViewController = controller;
return true;
}
}
}

Просмотреть файл

@ -0,0 +1,89 @@
//
// ImageProtocol.cs
//
// Author:
// Rolf Bjarne Kvinge (rolf@xamarin.com)
//
// Copyright 2012, Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace ImageProtocol
{
public class ImageProtocol : NSUrlProtocol
{
[Export ("canInitWithRequest:")]
public static bool canInitWithRequest (NSUrlRequest request)
{
return request.Url.Scheme == "custom";
}
[Export ("canonicalRequestForRequest:")]
public static new NSUrlRequest GetCanonicalRequest (NSUrlRequest forRequest)
{
return forRequest;
}
[Export ("initWithRequest:cachedResponse:client:")]
public ImageProtocol (NSUrlRequest request, NSCachedUrlResponse cachedResponse, NSUrlProtocolClient client)
: base (request, cachedResponse, client)
{
}
public override void StartLoading ()
{
var value = Request.Url.Path.Substring (1);
using (var image = Render (value)) {
using (var response = new NSUrlResponse (Request.Url, "image/jpeg", -1, null)) {
using (var data = image.AsJPEG ()) {
Client.ReceivedResponse (this, response, NSUrlCacheStoragePolicy.NotAllowed);
Client.DataLoaded (this, data);
Client.FinishedLoading (this);
}
}
}
}
public override void StopLoading ()
{
}
static UIImage Render (string value)
{
NSString text = new NSString (string.IsNullOrEmpty (value) ? " " : value);
UIFont font = UIFont.SystemFontOfSize (20);
SizeF size = text.StringSize (font);
UIGraphics.BeginImageContextWithOptions (size, false, 0.0f);
UIColor.Red.SetColor ();
text.DrawString (new PointF (0, 0), font);
UIImage image = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return image;
}
}
}

Просмотреть файл

@ -0,0 +1,108 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{C7A10540-758C-4EB8-AE9E-F77D289F139D}</ProjectGuid>
<ProjectTypeGuids>{6BC8ED88-2882-458C-8E55-DFD12B67127B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Exe</OutputType>
<RootNamespace>ImageProtocol</RootNamespace>
<AssemblyName>ImageProtocol</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\iPhoneSimulator\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<MtouchDebug>true</MtouchDebug>
<MtouchProfiling>true</MtouchProfiling>
<MtouchLink>None</MtouchLink>
<MtouchI18n />
<MtouchArch>ARMv7</MtouchArch>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhoneSimulator\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<MtouchLink>None</MtouchLink>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhone' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\iPhone\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchDebug>true</MtouchDebug>
<MtouchProfiling>true</MtouchProfiling>
<IpaPackageName />
<CrashReportingApiKey />
<MtouchI18n />
<MtouchArch>ARMv7</MtouchArch>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<CodesignKey>iPhone Developer</CodesignKey>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Ad-Hoc|iPhone' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\Ad-Hoc</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<BuildIpa>true</BuildIpa>
<CodesignKey>iPhone Distribution</CodesignKey>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'AppStore|iPhone' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\AppStore</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<CodesignKey>iPhone Distribution</CodesignKey>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="monotouch" />
</ItemGroup>
<ItemGroup>
<None Include="Info.plist" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" />
<Compile Include="ImageProtocol.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Content Include="test.html" />
<Content Include="icons\114_icon.png" />
<Content Include="icons\29_icon.png" />
<Content Include="icons\50_icon.png" />
<Content Include="icons\57_icon.png" />
<Content Include="icons\58_icon.png" />
<Content Include="icons\72_icon.png" />
<Content Include="icons\144_icon.png" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageProtocol", "ImageProtocol.csproj", "{C7A10540-758C-4EB8-AE9E-F77D289F139D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|iPhoneSimulator = Debug|iPhoneSimulator
Release|iPhoneSimulator = Release|iPhoneSimulator
Debug|iPhone = Debug|iPhone
Release|iPhone = Release|iPhone
Ad-Hoc|iPhone = Ad-Hoc|iPhone
AppStore|iPhone = AppStore|iPhone
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.AppStore|iPhone.ActiveCfg = AppStore|iPhone
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.AppStore|iPhone.Build.0 = AppStore|iPhone
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Debug|iPhone.ActiveCfg = Debug|iPhone
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Debug|iPhone.Build.0 = Debug|iPhone
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Release|iPhone.ActiveCfg = Release|iPhone
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Release|iPhone.Build.0 = Release|iPhone
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
{C7A10540-758C-4EB8-AE9E-F77D289F139D}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = ImageProtocol.csproj
EndGlobalSection
EndGlobal

40
ImageProtocol/Info.plist Normal file
Просмотреть файл

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDisplayName</key>
<string>Image Protocol Sample</string>
<key>CFBundleIconFiles</key>
<array>
<string>icons/57_icon.png</string>
<string>icons/114_icon.png</string>
<string>icons/72_icon.png</string>
<string>icons/144_icon.png</string>
</array>
<key>CFBundleIdentifier</key>
<string>com.xamarin.imageprotocolsample</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>5.0</string>
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
<integer>2</integer>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>

42
ImageProtocol/Main.cs Normal file
Просмотреть файл

@ -0,0 +1,42 @@
//
// AppDelegate.cs
//
// Author:
// Rolf Bjarne Kvinge (rolf@xamarin.com)
//
// Copyright 2012, Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace ImageProtocol
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args, null, "AppDelegate");
}
}
}

11
ImageProtocol/README.md Normal file
Просмотреть файл

@ -0,0 +1,11 @@
ImageProtocol
=============
This sample illustrates how to register a custom NSUrlProtocol.
Note that this sample requires at least MonoTouch 5.3.3
Authors
-------
Rolf Bjarne Kvinge

Двоичные данные
ImageProtocol/Screenshots/ImageProtocol01.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 148 KiB

Двоичные данные
ImageProtocol/icons/114_icon.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 20 KiB

Двоичные данные
ImageProtocol/icons/144_icon.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 27 KiB

Двоичные данные
ImageProtocol/icons/29_icon.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 2.9 KiB

Двоичные данные
ImageProtocol/icons/50_icon.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 5.8 KiB

Двоичные данные
ImageProtocol/icons/57_icon.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 7.1 KiB

Двоичные данные
ImageProtocol/icons/58_icon.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 7.3 KiB

Двоичные данные
ImageProtocol/icons/72_icon.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 10 KiB

32
ImageProtocol/test.html Normal file
Просмотреть файл

@ -0,0 +1,32 @@
<html>
<head>
<title>NSUrlProtocol Example</title>
</head>
<body>
<img src="custom:///NSUrlProtocol%20Example">
<!-- we have implemented a special NSURLProtocol that uses the 'special'
scheme to identify urls it is capable of handling. Our protocol simply
renders the path part of the URL into a jpeg image. We use this scheme
here to produce images in the html. -->
<form action="" method="get" onSubmit="return false;">
<p>Type your text here:<br>
<input name="exampletext" id="stringtext" type="text" size="21" onKeyUp="NewText();" value="Example Text"></p>
<img src="custom:///Example%20Text" id="stringimage">
</form>
<script type="text/javascript" language="javascript">
function NewText() {
var textField = document.getElementById('stringtext');
var image = document.getElementById('stringimage');
var theNewURL = 'custom:///' + escape(textField.value);
image.src = theNewURL;
}
</script>
</body>
</html>