系统至ios6之后,关于图片拉伸的方法已经扩展至3个函数:
1.ios4提供的方法:
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是不拉伸区域距离左边框的宽度,第二个参数是不拉伸区域距离上边框的宽度,其操作本质是对一个像素的复制拉伸,故没有渐变效果,这也是其缺点所在。
参数的意义是,如果参数指定10,5。那么,图片左边10个点,上边5个点。不会被拉伸,x坐标为11的点会被横向复制,y坐标为6的点会被纵向复制。注意:只是对一个点像素进行复制到指定的宽度。
2.ios5提供的方法
- (UIImage *)resizableImageCapInsets:(UIEdgeInsets)Insets
其中Insets这个参数的格式是(top,left,bottom,right),从上、左、下、右分别在图片上画了一道线,这样就给一个图片指定了一个矩形区域。只有在框里面的部分才会被拉伸,而框外面的部分则保持改变。比如(20,5,10,5),意思是下图矩形里面的部分可以被拉伸,而其余部分不变。
3.ios6提供的方法:
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
关于Insets参数,与ios5是相同的,不同的是其后增加了一个拉伸的模式,ios6.0的版本提供了
UIImageResizingModeTile和
UIImageResizingModeStretch两种模式,从名字就可以看出,是平铺模式和拉伸模式。平铺就是复制你Insets指定的矩形区域块来填充你所指定的图片区域,而拉伸就是通过拉伸你Insets指定的矩形区域块来填充你
所需的图片区域。我想,相较4.0的进步你也看出来了,是明显的吧,相较于以前的,图片的resize由一个点变成了一个矩形块,这样你的所指定块的渐变效果,也是可以呈现出来的。
只是,如果你需要兼容4.0的机器的话,那么还是需用老的函数来完成对图片的resize操作的。
在WinForm时代,我们不能忘记OpenFileDialog,那么,在windows store应用中,又有什么组件功能与之相似呢?
它就是Windows.Storage.Pickers.FileOpenPicker,其实,从类的名字我们同样可以知道它的用途,看来,
学会几个单词是很必要的哦。
FileOpenPicker可以选择单个文件(单选)或多个文件(多选),选择文件提交后,它会返回一个Windows.Storage.StorageFile,这个类你可以认为它就表示一个文件,后面我们会认识它,现在先卖个关子。
那么,具体怎么操作呢?其实,学习一个新东西,世界上没有比实例更直观的东西了。
现在,跟着我的步骤,你也来试试吧。(用什么语言你喜欢,我用CS)
1、启动VS for Win8,新建一个空白页面的App。
2、在主页MainPage.xaml中,放一个按钮button,和一个Image
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<Button Margin="6,1,3,5" Content="请选择一个文件" Click="onSelectFile"/>
<TextBlock Name="displayPath" Margin="12,1,0,5" FontSize="18"/>
</StackPanel>
<Image Grid.Row="1" Name="img" Stretch="Uniform" Margin="15"/>
</Grid>
3、打开代码文件MainPage.xaml.cs,其中代码如下。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
namespace OpenFilePickerSample
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void onSelectFile(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker(); //实例化一个FileOpenPicker
// 添加文件类型过滤,如.jpg,.txt等
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
// 设置初始路径
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
// PickMultipleFilesAsync是选取多个文件,
// PickSingleFileAsync选取单个文件
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
this.displayPath.Text = file.Path;
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
// 新建一个位图
Windows.UI.Xaml.Media.Imaging.BitmapImage bmp = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
bmp.SetSource(stream);
// 显示图片
this.img.Source = bmp;
}
}
}
}
SuggestedStartLocation属性表示,当我们启用文件选择器时显示哪个目录下的内容,它是一个枚举值。
要选取单个文件,应调用PickSingleFileAsync,若要选取多个文件,就调用PickMultipleFilesAsync方法,
这两个方法都是异步执行的,因此,需要添加await关键字。
按F5运行应用程序,点击“请选择一个文件”按钮,然后随便选一张图片,确定,返回,页面上就显示我
们刚才选的图片。
前提:需要系统签名或者在系统上mk编译。
public void reboot(){
Intent reboot = new Intent(Intent.ACTION_REBOOT);
reboot.setAction("android.intent.action.REBOOT");
reboot.putExtra("nowait", 1);
reboot.putExtra("interval", 1);
reboot.putExtra("window", 0);
sendBroadcast(reboot);
}