Permalink
Browse files

Added Flickr Supprot

  • Loading branch information...
1 parent 301e3dc commit d268f2ec343b31a54b70aba5391581bfc75d4c5f Miguel de Icaza committed Jan 10, 2010
View
@@ -2,31 +2,25 @@
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|iPhoneSimulator" ctype="Workspace" />
<MonoDevelop.Ide.DebuggingService>
<BreakpointStore>
- <Breakpoint file="/Users/miguel/Projects/OpenFlowSharp/OpenFlowSharp/OpenFlowView.cs" line="127" />
<Breakpoint file="/Users/miguel/Projects/OpenFlowSharp/OpenFlowSharp/OpenFlowView.cs" line="281" />
- <Breakpoint file="/Users/miguel/Projects/OpenFlowSharp/OpenFlowSharp/OpenFlowView.cs" line="463" />
</BreakpointStore>
</MonoDevelop.Ide.DebuggingService>
- <MonoDevelop.Ide.Workbench ActiveDocument="OpenFlowSharp/OpenFlowView.cs" ctype="Workbench">
+ <MonoDevelop.Ide.Workbench ActiveDocument="Sample/SampleViewController.xib.cs" ctype="Workbench">
<Files>
- <File FileName="../../../../Developer/MonoTouch/Source/mcs/class/corlib/System/Console.cs" Line="1" Column="1" />
- <File FileName="../../../../Developer/MonoTouch/Source/mcs/class/corlib/System/Object.cs" Line="1" Column="1" />
- <File FileName="../../../../Developer/MonoTouch/Source/mcs/class/corlib/System.Text/Encoding.cs" Line="1" Column="1" />
- <File FileName="../../../../Developer/MonoTouch/usr/bin/smcs" Line="1" Column="1" />
- <File FileName="../../../../cvs/iphone/monotouch/SystemConfiguration/NetworkReachability.cs" Line="1" Column="1" />
- <File FileName="Sample/SampleViewController.xib.cs" Line="38" Column="31" />
- <File FileName="OpenFlowSharp/OpenFlowView.cs" Line="38" Column="4" />
- <File FileName="../../../../Developer/MonoTouch/Source/mcs/class/System/System.Collections.Generic/Stack.cs" Line="152" Column="1" />
- <File FileName="OpenFlowSharp/ItemView.cs" Line="70" Column="6" />
+ <File FileName="Sample/SampleViewController.xib.cs" Line="68" Column="26" />
+ <File FileName="OpenFlowSharp/OpenFlowView.cs" Line="1" Column="1" />
+ <File FileName="../../../../Developer/MonoTouch/Source/mcs/class/System/System.Collections.Generic/Stack.cs" Line="1" Column="1" />
+ <File FileName="OpenFlowSharp/ItemView.cs" Line="1" Column="1" />
+ <File FileName="OpenFlowSharp/ImageUtils.cs" Line="1" Column="1" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State expanded="True">
<Node name="OpenFlowSharp" expanded="True">
<Node name="References" expanded="True" />
- <Node name="OpenFlowView.cs" selected="True" />
</Node>
<Node name="Sample" expanded="True">
+ <Node name="References" expanded="True" selected="True" />
<Node name="SampleViewController.xib" expanded="True" />
</Node>
</State>
Binary file not shown.
@@ -206,8 +206,8 @@ ItemView CoverForIndex (int index)
void UpdateCoverImage (ItemView aCover)
{
- var coverImage = coverImages [aCover.Number];
- if (coverImage != null){
+ UIImage coverImage;
+ if (coverImages.TryGetValue (aCover.Number, out coverImage)){
if (coverImageHeights.ContainsKey (aCover.Number))
aCover.SetImage (coverImage, coverImageHeights [aCover.Number], kReflectionFraction);
} else {
View
@@ -1 +1,130 @@
-<?xml version="1.0" encoding="utf-8"?><Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform> <ProductVersion>9.0.21022</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{BBBC1BB1-9A6B-452F-9894-0D4FD75D7921}</ProjectGuid> <ProjectTypeGuids>{E613F3A2-FE9C-494F-B74E-F63BCB86FEA6};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <OutputType>Exe</OutputType> <RootNamespace>Sample</RootNamespace> <MainNibFile>MainWindow.xib</MainNibFile> <AssemblyName>Sample</AssemblyName> <MtouchSdkVersion>3.0</MtouchSdkVersion> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> </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> <MtouchDebug>True</MtouchDebug> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' "> <DebugType>none</DebugType> <Optimize>false</Optimize> <OutputPath>bin\iPhoneSimulator\Release</OutputPath> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <MtouchDebug>False</MtouchDebug> <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> <MtouchDebug>True</MtouchDebug> <CodesignKey>iPhone Developer</CodesignKey> <MtouchLink>None</MtouchLink> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' "> <DebugType>none</DebugType> <Optimize>false</Optimize> <OutputPath>bin\iPhone\Release</OutputPath> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <MtouchDebug>False</MtouchDebug> <CodesignKey>iPhone Developer</CodesignKey> <MtouchLink>None</MtouchLink> </PropertyGroup> <ItemGroup> <Reference Include="System" /> <Reference Include="System.Xml" /> <Reference Include="System.Core" /> <Reference Include="monotouch" /> </ItemGroup> <ItemGroup> <Compile Include="MainWindow.xib.designer.cs"> <DependentUpon>MainWindow.xib</DependentUpon> </Compile> <Compile Include="Main.cs" /> <Compile Include="SampleViewController.xib.cs"> <DependentUpon>SampleViewController.xib</DependentUpon> </Compile> <Compile Include="SampleViewController.xib.designer.cs"> <DependentUpon>SampleViewController.xib</DependentUpon> </Compile> </ItemGroup> <ItemGroup> <Page Include="MainWindow.xib" /> <Page Include="SampleViewController.xib" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\OpenFlowSharp\OpenFlowSharp.csproj"> <Project>{A8CE55FC-1D12-4DB5-A5F8-836928F7BCD9}</Project> <Name>OpenFlowSharp</Name> </ProjectReference> </ItemGroup> <ItemGroup> <Content Include="default.png" /> <Content Include="images\0.jpg" /> <Content Include="images\1.jpg" /> <Content Include="images\2.jpg" /> <Content Include="images\3.jpg" /> <Content Include="images\4.jpg" /> <Content Include="images\5.jpg" /> <Content Include="images\6.jpg" /> <Content Include="images\7.jpg" /> <Content Include="images\8.jpg" /> <Content Include="images\9.jpg" /> <Content Include="images\10.jpg" /> <Content Include="images\11.jpg" /> <Content Include="images\12.jpg" /> <Content Include="images\13.jpg" /> <Content Include="images\14.jpg" /> <Content Include="images\15.jpg" /> <Content Include="images\16.jpg" /> <Content Include="images\17.jpg" /> <Content Include="images\18.jpg" /> <Content Include="images\19.jpg" /> <Content Include="images\20.jpg" /> <Content Include="images\21.jpg" /> <Content Include="images\22.jpg" /> <Content Include="images\23.jpg" /> <Content Include="images\24.jpg" /> <Content Include="images\25.jpg" /> <Content Include="images\26.jpg" /> <Content Include="images\27.jpg" /> <Content Include="images\28.jpg" /> <Content Include="images\29.jpg" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ProjectExtensions> <MonoDevelop> <Properties InternalTargetFrameworkVersion="IPhone" xmlns="" /> </MonoDevelop> </ProjectExtensions></Project>
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform>
+ <ProductVersion>9.0.21022</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{BBBC1BB1-9A6B-452F-9894-0D4FD75D7921}</ProjectGuid>
+ <ProjectTypeGuids>{E613F3A2-FE9C-494F-B74E-F63BCB86FEA6};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>Sample</RootNamespace>
+ <MainNibFile>MainWindow.xib</MainNibFile>
+ <AssemblyName>Sample</AssemblyName>
+ <MtouchSdkVersion>3.0</MtouchSdkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ </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>
+ <MtouchDebug>True</MtouchDebug>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
+ <DebugType>none</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\iPhoneSimulator\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <MtouchDebug>False</MtouchDebug>
+ <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>
+ <MtouchDebug>True</MtouchDebug>
+ <CodesignKey>iPhone Developer</CodesignKey>
+ <MtouchLink>None</MtouchLink>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
+ <DebugType>none</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\iPhone\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <MtouchDebug>False</MtouchDebug>
+ <CodesignKey>iPhone Developer</CodesignKey>
+ <MtouchLink>None</MtouchLink>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Xml" />
+ <Reference Include="System.Core" />
+ <Reference Include="monotouch" />
+ <Reference Include="flickrnet, Version=2.2.0.1868, Culture=neutral, PublicKeyToken=null">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>flickrnet.dll</HintPath>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="MainWindow.xib.designer.cs">
+ <DependentUpon>MainWindow.xib</DependentUpon>
+ </Compile>
+ <Compile Include="Main.cs" />
+ <Compile Include="SampleViewController.xib.cs">
+ <DependentUpon>SampleViewController.xib</DependentUpon>
+ </Compile>
+ <Compile Include="SampleViewController.xib.designer.cs">
+ <DependentUpon>SampleViewController.xib</DependentUpon>
+ </Compile>
+ </ItemGroup>
+ <ItemGroup>
+ <Page Include="MainWindow.xib" />
+ <Page Include="SampleViewController.xib" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\OpenFlowSharp\OpenFlowSharp.csproj">
+ <Project>{A8CE55FC-1D12-4DB5-A5F8-836928F7BCD9}</Project>
+ <Name>OpenFlowSharp</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="default.png" />
+ <Content Include="images\0.jpg" />
+ <Content Include="images\1.jpg" />
+ <Content Include="images\2.jpg" />
+ <Content Include="images\3.jpg" />
+ <Content Include="images\4.jpg" />
+ <Content Include="images\5.jpg" />
+ <Content Include="images\6.jpg" />
+ <Content Include="images\7.jpg" />
+ <Content Include="images\8.jpg" />
+ <Content Include="images\9.jpg" />
+ <Content Include="images\10.jpg" />
+ <Content Include="images\11.jpg" />
+ <Content Include="images\12.jpg" />
+ <Content Include="images\13.jpg" />
+ <Content Include="images\14.jpg" />
+ <Content Include="images\15.jpg" />
+ <Content Include="images\16.jpg" />
+ <Content Include="images\17.jpg" />
+ <Content Include="images\18.jpg" />
+ <Content Include="images\19.jpg" />
+ <Content Include="images\20.jpg" />
+ <Content Include="images\21.jpg" />
+ <Content Include="images\22.jpg" />
+ <Content Include="images\23.jpg" />
+ <Content Include="images\24.jpg" />
+ <Content Include="images\25.jpg" />
+ <Content Include="images\26.jpg" />
+ <Content Include="images\27.jpg" />
+ <Content Include="images\28.jpg" />
+ <Content Include="images\29.jpg" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <ProjectExtensions>
+ <MonoDevelop>
+ <Properties InternalTargetFrameworkVersion="IPhone" />
+ </MonoDevelop>
+ </ProjectExtensions>
+</Project>
View
Binary file not shown.
@@ -29,19 +29,98 @@
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using OpenFlowSharp;
+using System.Threading;
+using FlickrNet;
+using System.Drawing;
namespace Sample
{
public partial class SampleViewController : UIViewController, IOpenFlowDataSource
{
+ const string apiKey = "c0cf24ba43385203b331b578dcaa54eb";
+ const string sharedSecret = "670547d41098cd97";
+ Flickr flickr;
+ Photos photos;
+
OpenFlowView flowView;
+ AutoResetEvent signal = new AutoResetEvent (false);
+ Queue<NSAction> tasks = new Queue<NSAction> ();
#region IOpenFlowDataSource implementation
+ UIImage PrepareFlickrPhoto (UIImage image, SizeF cropSize)
+ {
+ // First rescale
+ var rect = new RectangleF (0, 0, cropSize.Width, cropSize.Height);
+ UIGraphics.BeginImageContext (rect.Size);
+ image.Draw (rect);
+ var scaledImage = UIGraphics.GetImageFromCurrentImageContext ();
+ UIGraphics.EndImageContext ();
+
+ // Now crop
+ var cropRect = new RectangleF ((scaledImage.Size.Width-cropSize.Width)/2,
+ (scaledImage.Size.Height-cropSize.Height)/2,
+ cropSize.Width, cropSize.Height);
+
+ UIGraphics.BeginImageContext (cropRect.Size);
+ var ctx = UIGraphics.GetCurrentContext ();
+
+ // Compensate for Quartz coordinates
+ ctx.TranslateCTM (0.0f, cropRect.Size.Height);
+ ctx.ScaleCTM (1, -1);
+
+ // Draw view into context
+ ctx.DrawImage (new RectangleF (-cropRect.X, cropRect.Y - (image.Size.Height - cropRect.Size.Height), image.Size.Width, image.Size.Height), image.CGImage);
+
+ // Create UIImage from context
+ var newImage = UIGraphics.GetImageFromCurrentImageContext ();
+ UIGraphics.EndImageContext ();
+
+ return newImage;
+ }
+
+ SizeF CalculateSizeForCroppingBox (UIImage image, int width, int height)
+ {
+ float newHeight, newWidth;
+
+ if (image.Size.Width < image.Size.Height){
+ newWidth = width;
+ newHeight = width * (image.Size.Height / image.Size.Width);
+ } else {
+ newHeight = height;
+ newWidth = height * (image.Size.Width / image.Size.Height);
+ }
+ return new SizeF (newWidth, newHeight);
+ }
+
void IOpenFlowDataSource.RequestImage (OpenFlowView view, int index)
{
- // This is only used for providing data on demand, instead
- // of preloading all the data
- throw new NotImplementedException ();
+ NSAction task;
+
+ if (flickr == null){
+ task = delegate {
+ var img = UIImage.FromFile ("images/" + index + ".jpg");
+ InvokeOnMainThread (delegate {
+ flowView [index] = img;
+ });
+ };
+ } else {
+ task = delegate {
+ var data = NSData.FromUrl (new NSUrl (photos [index].SmallUrl));
+ var image = UIImage.LoadFromData (data);
+
+ if (image != null){
+ InvokeOnMainThread (delegate {
+ image = PrepareFlickrPhoto (image, CalculateSizeForCroppingBox (image, 255, 255));
+
+ flowView [index] = image;
+ });
+ }
+ };
+ }
+ lock (tasks){
+ tasks.Enqueue (task);
+ }
+ signal.Set ();
}
@@ -73,32 +152,66 @@ void Initialize ()
flowView = new OpenFlowView (UIScreen.MainScreen.Bounds, this);
View.AddSubview (flowView);
- // I am lazy, do not want to implement the 2 other samples.
-
- LoadAllImages ();
- return;
-
using (var alertView = new UIAlertView ("OpenFlowSharp Demo Data Source",
"Would you like to download images from Flickr or use 30 sample images included with this project?",
null, "Flickr",
"Samples (all at once)",
"Samples (using threads)")){
alertView.Dismissed += delegate(object sender, UIButtonEventArgs e) {
switch (e.ButtonIndex){
+ // Flickr
case 0:
- // TODO
+ flickr = new Flickr (apiKey, sharedSecret);
+ new Thread (delegate (object k) {
+ try {
+ photos = flickr.InterestingnessGetList ();
+ InvokeOnMainThread (delegate {
+ flowView.NumberOfImages = photos.Count;
+ new Thread (Worker).Start ();
+ });
+ } catch {
+ using (var alert = new UIAlertView ("Error", "While accessing Flickr", null, "Ok")){
+ alert.Show ();
+ }
+ }
+ }).Start ();
+ break;
+
+ // Sync case, load all images at startup
case 1:
LoadAllImages ();
break;
+
+ // Load images on demand on a worker thread
case 2:
flowView.NumberOfImages = 30;
+ new Thread (Worker).Start ();
break;
}
};
alertView.Show ();
}
}
+ //
+ // Dispatches the tasks queued in the tasks queue
+ //
+ void Worker ()
+ {
+ while (signal.WaitOne ()){
+ while (true){
+ NSAction task;
+
+ lock (tasks){
+ if (tasks.Count > 0)
+ task = tasks.Dequeue ();
+ else
+ break;
+ }
+ task ();
+ }
+ }
+ }
#endregion
}
}
View
Binary file not shown.

0 comments on commit d268f2e

Please sign in to comment.