Added ios7 UIImageEffects sample

This commit is contained in:
Miguel de Icaza 2014-01-16 22:14:55 -05:00
Родитель 67c62d48ff
Коммит b6397ea845
16 изменённых файлов: 503 добавлений и 0 удалений

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

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace UIImageEffects
{
public class EffectsViewController : UIViewController {
UIImageView imageView;
UIImage sourceImage;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
imageView = new UIImageView (View.Bounds) {
UserInteractionEnabled = true
};
imageView.AddGestureRecognizer (new UITapGestureRecognizer ((x)=> {
UpdateImage ();
}));
View.AddSubview (imageView);
sourceImage = UIImage.FromBundle ("DisplayImage");
Console.WriteLine ("Got: {0}", sourceImage);
UpdateImage ();
var alert = new UIAlertView ("Tap to change image effect", "Tap on the change", null, "Dismiss");
alert.Show ();
}
int effectIndex;
void UpdateImage ()
{
UIImage effectImage;
if (effectIndex > 5)
effectIndex = 0;
switch (effectIndex) {
case 1:
effectImage = sourceImage.ApplyLightEffect ();
break;
case 2:
effectImage = sourceImage.ApplyExtraLightEffect ();
break;
case 3:
effectImage = sourceImage.ApplyDarkEffect ();
break;
case 4:
effectImage = sourceImage.ApplyTintEffect (UIColor.Blue);
break;
case 5:
effectImage = sourceImage.ApplyTintEffect (UIColor.Green);
break;
case 0:
default:
effectImage = sourceImage;
break;
}
imageView.Image = effectImage;
effectIndex++;
}
}
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = new EffectsViewController ();
window.MakeKeyAndVisible ();
return true;
}
static void Main (string[] args)
{
UIApplication.Main (args, null, "AppDelegate");
}
}
}

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

@ -0,0 +1,7 @@
{
"images":[ ],
"info":{
"version":1,
"author":"xcode"
}
}

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

@ -0,0 +1,40 @@
{
"images": [
{
"scale": "1x",
"idiom": "universal"
},
{
"scale": "2x",
"idiom": "universal"
},
{
"filename": "cheetah480.png",
"scale": "1x",
"idiom": "iphone"
},
{
"filename": "cheetah960.png",
"scale": "2x",
"idiom": "iphone"
},
{
"filename": "cheetah1136.png",
"subtype": "retina4",
"scale": "2x",
"idiom": "iphone"
},
{
"scale": "1x",
"idiom": "ipad"
},
{
"scale": "2x",
"idiom": "ipad"
}
],
"info": {
"version": 1,
"author": "xcode"
}
}

Двоичные данные
UIImageEffects/Images.xcassets/DisplayImage.imageset/cheetah1136.png поставляемый Normal file

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

После

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

Двоичные данные
UIImageEffects/Images.xcassets/DisplayImage.imageset/cheetah480.png поставляемый Normal file

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

После

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

Двоичные данные
UIImageEffects/Images.xcassets/DisplayImage.imageset/cheetah960.png поставляемый Normal file

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

После

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

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

@ -0,0 +1,7 @@
{
"images":[ ],
"info":{
"version":1,
"author":"xcode"
}
}

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

@ -0,0 +1,18 @@
<?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>UIDeviceFamily</key>
<array>
<integer>1</integer>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>MinimumOSVersion</key>
<string>6.0</string>
</dict>
</plist>

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

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<SampleMetadata>
<ID>508579D3-C90F-4E18-8308-1B2A4CFD6D77</ID>
<IsFullApplication>true</IsFullApplication>
<Level>Intermediate</Level>
<Tags>User Interface, Drawing, Accelerate, Graphics</Tags>
</SampleMetadata>

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

@ -0,0 +1,26 @@
This is a port of Apple's UIImageEffect sample
----------------------------------------------
From Apple:
UIImageEffects shows how to create and apply blur and tint
effects to an image using the vImage, Quartz, and UIKit
frameworks. The vImage framework is suited for
high-performance image processing. Using vImage, y our app
gets all the benefits of vector processing without the need
for you to write vectorized code.
Main classes:
AppDelegate.cs:
Sets up the application, loads the image and calls the
various filters on each tap.
UIImageEffect.cs:
Contains various extension methods that allow the user
to blur images and tint those images. The convenience
methods show how to use the API.
Port by Miguel de Icaza

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

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

После

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

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

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

После

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

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

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

После

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

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

@ -0,0 +1,167 @@
//
// UIImageEffects.cs: extensio methods on UIImage to lighten, darken, tint and blur images.
//
//
//
using System;
using MonoTouch.UIKit;
using MonoTouch.Accelerate;
using System.Diagnostics;
using System.Drawing;
using MonoTouch.CoreGraphics;
namespace UIImageEffects
{
public static class UIImageEffects
{
public static UIImage ApplyLightEffect (this UIImage self)
{
var tintColor = UIColor.FromWhiteAlpha (1.0f, 0.3f);
return ApplyBlur (self, blurRadius:30, tintColor:tintColor, saturationDeltaFactor:1.8f, maskImage:null);
}
public static UIImage ApplyExtraLightEffect (this UIImage self)
{
var tintColor = UIColor.FromWhiteAlpha (0.97f, 0.82f);
return ApplyBlur (self, blurRadius:20, tintColor:tintColor, saturationDeltaFactor:1.8f, maskImage:null);
}
public static UIImage ApplyDarkEffect (this UIImage self)
{
var tintColor = UIColor.FromWhiteAlpha (0.11f, 0.73f);
return ApplyBlur (self, blurRadius:20, tintColor:tintColor, saturationDeltaFactor:1.8f, maskImage:null);
}
public static UIImage ApplyTintEffect (this UIImage self, UIColor tintColor)
{
const float EffectColorAlpha = 0.6f;
var effectColor = tintColor;
float alpha;
var componentCount = tintColor.CGColor.NumberOfComponents;
if (componentCount == 2) {
float white;
if (tintColor.GetWhite (out white, out alpha))
effectColor = UIColor.FromWhiteAlpha (white, EffectColorAlpha);
} else {
try {
float r, g, b;
tintColor.GetRGBA (out r, out g, out b, out alpha);
effectColor = UIColor.FromRGBA (r, g, b, EffectColorAlpha);
} catch {
}
}
return ApplyBlur (self, blurRadius: 10, tintColor: effectColor, saturationDeltaFactor: -1, maskImage: null);
}
public unsafe static UIImage ApplyBlur (UIImage image, float blurRadius, UIColor tintColor, float saturationDeltaFactor, UIImage maskImage)
{
if (image.Size.Width < 1 || image.Size.Height < 1) {
Debug.WriteLine (@"*** error: invalid size: ({0} x {1}). Both dimensions must be >= 1: {2}", image.Size.Width, image.Size.Height, image);
return null;
}
if (image.CGImage == null) {
Debug.WriteLine (@"*** error: image must be backed by a CGImage: {0}", image);
return null;
}
if (maskImage != null && maskImage.CGImage == null) {
Debug.WriteLine (@"*** error: maskImage must be backed by a CGImage: {0}", maskImage);
return null;
}
var imageRect = new RectangleF (PointF.Empty, image.Size);
var effectImage = image;
bool hasBlur = blurRadius > float.Epsilon;
bool hasSaturationChange = Math.Abs (saturationDeltaFactor - 1) > float.Epsilon;
if (hasBlur || hasSaturationChange) {
UIGraphics.BeginImageContextWithOptions (image.Size, false, UIScreen.MainScreen.Scale);
var contextIn = UIGraphics.GetCurrentContext ();
contextIn.ScaleCTM (1.0f, -1.0f);
contextIn.TranslateCTM (0, -image.Size.Height);
contextIn.DrawImage (imageRect, image.CGImage);
var effectInContext = contextIn.AsBitmapContext () as CGBitmapContext;
var effectInBuffer = new vImageBuffer () {
Data = effectInContext.Data,
Width = effectInContext.Width,
Height = effectInContext.Height,
BytesPerRow = effectInContext.BytesPerRow
};
UIGraphics.BeginImageContextWithOptions (image.Size, false, UIScreen.MainScreen.Scale);
var effectOutContext = UIGraphics.GetCurrentContext ().AsBitmapContext () as CGBitmapContext;
var effectOutBuffer = new vImageBuffer () {
Data = effectOutContext.Data,
Width = effectOutContext.Width,
Height = effectOutContext.Height,
BytesPerRow = effectOutContext.BytesPerRow
};
if (hasBlur) {
var inputRadius = blurRadius * UIScreen.MainScreen.Scale;
uint radius = (uint)(Math.Floor (inputRadius * 3 * Math.Sqrt (2 * Math.PI) / 4 + 0.5));
if ((radius % 2) != 1)
radius += 1;
vImage.BoxConvolveARGB8888 (ref effectInBuffer, ref effectOutBuffer, IntPtr.Zero, 0, 0, radius, radius, Pixel8888.Zero, vImageFlags.EdgeExtend);
vImage.BoxConvolveARGB8888 (ref effectOutBuffer, ref effectInBuffer, IntPtr.Zero, 0, 0, radius, radius, Pixel8888.Zero, vImageFlags.EdgeExtend);
vImage.BoxConvolveARGB8888 (ref effectInBuffer, ref effectOutBuffer, IntPtr.Zero, 0, 0, radius, radius, Pixel8888.Zero, vImageFlags.EdgeExtend);
}
bool effectImageBuffersAreSwapped = false;
if (hasSaturationChange) {
var s = saturationDeltaFactor;
var floatingPointSaturationMatrix = new float [] {
0.0722f + 0.9278f * s, 0.0722f - 0.0722f * s, 0.0722f - 0.0722f * s, 0,
0.7152f - 0.7152f * s, 0.7152f + 0.2848f * s, 0.7152f - 0.7152f * s, 0,
0.2126f - 0.2126f * s, 0.2126f - 0.2126f * s, 0.2126f + 0.7873f * s, 0,
0, 0, 0, 1,
};
const int divisor = 256;
var saturationMatrix = new short [floatingPointSaturationMatrix.Length];
for (int i = 0; i < saturationMatrix.Length; i++)
saturationMatrix [i] = (short)Math.Round (floatingPointSaturationMatrix [i] * divisor);
if (hasBlur) {
vImage.MatrixMultiplyARGB8888 (ref effectOutBuffer, ref effectInBuffer, saturationMatrix, divisor, null, null, vImageFlags.NoFlags);
effectImageBuffersAreSwapped = true;
} else
vImage.MatrixMultiplyARGB8888 (ref effectInBuffer, ref effectOutBuffer, saturationMatrix, divisor, null, null, vImageFlags.NoFlags);
}
if (!effectImageBuffersAreSwapped)
effectImage = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
if (effectImageBuffersAreSwapped)
effectImage = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
}
// Setup up output context
UIGraphics.BeginImageContextWithOptions (image.Size, false, UIScreen.MainScreen.Scale);
var outputContext = UIGraphics.GetCurrentContext ();
outputContext.ScaleCTM (1, -1);
outputContext.TranslateCTM (0, -image.Size.Height);
// Draw base image
if (hasBlur) {
outputContext.SaveState ();
if (maskImage != null)
outputContext.ClipToMask (imageRect, maskImage.CGImage);
outputContext.DrawImage (imageRect, effectImage.CGImage);
outputContext.RestoreState ();
}
if (tintColor != null) {
outputContext.SaveState ();
outputContext.SetFillColor (tintColor.CGColor);
outputContext.FillRect (imageRect);
outputContext.RestoreState ();
}
var outputImage = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return outputImage;
}
}
}

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

@ -0,0 +1,113 @@
<?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>{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}</ProjectGuid>
<ProjectTypeGuids>{6BC8ED88-2882-458C-8E55-DFD12B67127B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Exe</OutputType>
<RootNamespace>UIImageEffects</RootNamespace>
<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
<AssemblyName>UIImageEffects</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>
<MtouchLink>None</MtouchLink>
<ConsolePause>false</ConsolePause>
<MtouchDebug>true</MtouchDebug>
<MtouchI18n>
</MtouchI18n>
<MtouchArch>ARMv7</MtouchArch>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhoneSimulator\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<MtouchLink>None</MtouchLink>
<ConsolePause>false</ConsolePause>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</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>
<MtouchDebug>true</MtouchDebug>
<ConsolePause>false</ConsolePause>
<CodesignKey>iPhone Developer</CodesignKey>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<ConsolePause>false</ConsolePause>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Ad-Hoc|iPhone' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\Ad-Hoc</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Distribution</CodesignKey>
<BuildIpa>true</BuildIpa>
<ConsolePause>false</ConsolePause>
<CodesignProvision>Automatic:AdHoc</CodesignProvision>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'AppStore|iPhone' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\AppStore</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Distribution</CodesignKey>
<ConsolePause>false</ConsolePause>
<CodesignProvision>Automatic:AppStore</CodesignProvision>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="monotouch" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
<Folder Include="Images.xcassets\DisplayImage\" />
</ItemGroup>
<ItemGroup>
<None Include="Info.plist" />
</ItemGroup>
<ItemGroup>
<Compile Include="AppDelegate.cs" />
<Compile Include="UIImageEffects.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<ImageAsset Include="Images.xcassets\AppIcons.appiconset\Contents.json" />
<ImageAsset Include="Images.xcassets\LaunchImages.launchimage\Contents.json" />
<ImageAsset Include="Images.xcassets\DisplayImage.imageset\Contents.json" />
<ImageAsset Include="Images.xcassets\DisplayImage.imageset\cheetah480.png" />
<ImageAsset Include="Images.xcassets\DisplayImage.imageset\cheetah960.png" />
<ImageAsset Include="Images.xcassets\DisplayImage.imageset\cheetah1136.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}") = "UIImageEffects", "UIImageEffects.csproj", "{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}"
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
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.AppStore|iPhone.ActiveCfg = AppStore|iPhone
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.AppStore|iPhone.Build.0 = AppStore|iPhone
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Debug|iPhone.ActiveCfg = Debug|iPhone
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Debug|iPhone.Build.0 = Debug|iPhone
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Release|iPhone.ActiveCfg = Release|iPhone
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Release|iPhone.Build.0 = Release|iPhone
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
{CBD5A17E-2279-413A-B2C2-CB99EC2263B2}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = UIImageEffects.csproj
EndGlobalSection
EndGlobal