每天一点iOS知识回顾6

1.UIImage初始化一张图片有几种方法?简述各自的优缺点。

1、从资源读取

UIImage *image = [UIImage imageNamed:@”1.png”];
2、从网络读取
NSURL*url=[NSURL URLWithString:@"http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"];
3.从手机本地读取
//读取本地图片非resource
NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];
4.从现有的context中获得图像
//add ImageIO.framework and #import <ImageIO/ImageIO.h>
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
CGContextRef ctx=UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
//transformCTM的2种方式
//CGContextConcatCTM(ctx, CGAffineTransformMakeScale(.2, -0.2));
//CGContextScaleCTM(ctx,1,-1);
//注意坐标要反下,用ctx来作为图片源
CGImageRef capture=CGBitmapContextCreateImage(ctx);
CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);
CGContextDrawImage(ctx, CGRectMake(160, 230, 160, 230), img);
CGImageRef capture2=CGBitmapContextCreateImage(ctx);
5、用Quartz的CGImageSourceRef来读取图片
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
参考(http://blog.csdn.net/jerryvon/article/details/7526147#)

2.回答person的retainCount值,并解释为什么

Person * per = [[Person alloc] init];
self.person = per;
答案:person属性如果为assign的话retainCount为1,如果为retain的话retainCount为2

3.这段代码有什么问题吗:

@implementation Person
- (void)setAge:(int)newAge {
self.age = newAge;
}
@end
不能在setAge:方法中使用self.age  =  newAge,相当于在setAge:方法中调用 [self  setAge:newAge ], 出现死循环,

4.这段代码有什么问题,如何修改

for (int i = 0; i < someLargeNumber; i++) {
NSString *string = @”Abc”;
string = [string lowercaseString];
string = [string stringByAppendingString:@"xyz"];
NSLog(@“%@”, string);
}会出现内存泄露:修改后:
for(int i=0;i<1000;i++){
NSAutoreleasePool *pool= [[NSAutoreleasePool alloc]init];
NSString *string=@”Abc”;
String=[string lowercaseString];
String=[string stringByAppendingString:@”xyz”];
NSLog(“%@”,string);
//释放池
[pool drain];
}

5.截取字符串”20 | http://www.baidu.com”中,”|”字符前面和后面的数据,分别输出它们。

NSString * str = @"20 | http://www.baidu.com";
NSArray *array = [str componentsSeparatedByString:@"|"]; //这是分别输出的截取后的字符串
for (int i = 0; i<[array count]; ++i) { NSLog(@"%d=%@",i,[array objectAtIndex:i]);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值