从网络上获取image,添加到imageview中,及若干问题

本文介绍了如何从网络获取图片并将其显示在 iOS 的 UIImageView 中的方法。此外,还讨论了 NSAutoreleasePool 的使用场景,并分享了 textField 自动收起键盘的实现方式以及设置随机背景色的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从网络上获取image,添加到imageview中: NSString *strings = [NSString stringWithString: @"http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"]; NSData *date = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: strings]]; UIImageView *image = [[UIImageView alloc] initWithImage: [UIImage imageWithData: date]]; image.frame = CGRectMake(0, 0, 100, 40); [self.view addSubview: image];

NSAutoreleasePool的作用: for (int i=0;i<20;i++) { NSAutoreleasePool *aa = [[NSAutoreleasePool alloc] init]; str = [[[NSString alloc] initWithFormat: @"%d", i] autorelease]; NSLog( @"str is %@" , str ); [aa release]; }

在设置textField的时候,忘记了 textField.delegate = self 所以键盘没有自动消失. - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } 也没有被调用…

在设置背景色的时候,后面的随机数少写了".0",就悲剧了,一直没有出现正确的结果,其实他们接受的是CGFloat类型的值,填写后,ok了. self.window.backgroundColor = [UIColor colorWithRed: arc4random() % 255/255.0 green: arc4random() % 255/255.0 blue: arc4random() % 255/255.0 alpha: 1.0];

这些个问题,以前都做过,不过很多细节没有很好的掌握,以至于在又时间要求的情况下,不能很快的产生代码,其实还是对这些知识的熟练度不够.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值