Archive for the ‘program’ Category
在iphone app中添加音量控制条
1. 添加MediaPlayer.framework
2. #import <MediaPlayer/MPVolumeView.h>
3. 代码
MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:CGRectMake(18, 380, 284, 23)] autorelease]; volumeView.center = CGPointMake(160,380); [volumeView sizeToFit]; [self.view addSubview:volumeView];
iphone程序中跳转到appstore评论界面
在程序中加入appstore的review跳转,实现方法很简单,就是让系统去打开一个review的链接,代码如下:
- (void) gotoWriteReviews { NSString* appID = NSLocalizedString(@"itunesconnect_appid", nil); //这里是itunes connect生成的一串数字id NSString *u = [NSString stringWithFormat:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software",appID]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:u]]; }
在图片上盖一层mask
- (UIImage*) createImage:(UIImage*)_image withMask:(UIImage*)_mask { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, _image.size.width, _image.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); CGRect rect = CGRectMake(0, 0, _image.size.width, _image.size.height); UIColor *bColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; CGContextSetFillColorWithColor(context, [bColor CGColor]); //黑色是不透明,白色是透明 CGContextFillRect(context, rect); CGContextClipToMask(context, rect, [_mask CGImage]); CGContextDrawImage(context, rect, [_image CGImage]); CGImageRef newImage = CGBitmapContextCreateImage(context); CGContextRelease(context); UIImage* _newImage = [[UIImage alloc] initWithCGImage:newImage]; CGImageRelease(newImage); return [_newImage autorelease]; }
最先创建了一个灰度的颜色空间导致合成出来的mask颜色都是反的,黑色的显出白色,后来发现一篇例子是用RGB颜色空间来做再自己加上黑色填充后就得到了想要的效果,效果图:

在UIViewController中用多线程更新UI的异常
在UIViewController中用多线程时,如果该线程方法中有更新UI操作的话,会抛出如下错误:
Tried to obtain the web lock from a thread other than the main thread or the web thread.
This may be a result of calling to UIKit from a secondary thread. Crashing now…
貌似是因为UIKit是非线程安全的,所以更新UI只能在主线程中操作,解决的办法:
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
用该方法指定要执行的UI更新Method在主线程中执行.
json for objective-c
一个json对象和NSDictionary对象相互转换的framework
project home:
http://code.google.com/p/json-framework/
iphone状态栏网络标志
控制状态栏网络标志(就是有数据连接时显示一个转阿转的小图标)
//显示
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//隐藏
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
log4j配置环境变量迂回法
在log4j.properties中配置输入文件
log4j.appender.A1.File= ${JBOSS_HOME}/server/default/log/my.log
JAVA中加载配置
//从系统环境变量中取出JBOSS_HOME路径设置为系统property
System.setProperty(“JBOSS_HOME”, System.getenv(“JBOSS_HOME”));
//读取log4j配置
PropertyConfigurator.configure(this.class.getResource(“/”).getPath() + “log4j.properties”);
logger = Logger.getLogger(this.class);
相册登场
.net读取Excel文件中的图片
首先要在项目中引入一个Excel的COM组件excel.dll
假设C盘根目录下存在a.xls的文件,在单元格第2行第2列存在要读取的图片
//引用Excel命名空间
using Excel;//......
//下面从a.xls中的2,2格复制图片到剪贴板,然后从剪贴板读取图片并显示到pictureBox1中。
private void btnGetImageFromExcel_Click(object sender, EventArgs e)
{
//初始化excel对象
Excel.Application excel = new Excel.Application();
//打开xls文件(注意:后面的参数都用Type.Missing填充,表示使用参数的默认值)
excel.Workbooks.Open(@"C:\a.xls", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
//选定到第2行第2列所在的单元格
Range r = (Range)excel.Cells[2, 2];
r.Select();
//将单元格复制到剪贴板中
r.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
//退出excel
excel.Quit();
//判断剪贴板中是否存在图片,如果存在,则将图片显示到pictureBox1中
if (Clipboard.ContainsImage())
{
Image image=Clipboard.GetImage();
pictureBox1.Image = image;
}
}