`
yourgame
  • 浏览: 352539 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

iphone移动加载网络图片

    博客分类:
  • IOS
阅读更多
@interface AsyncImageView: UIView {
    NSURLConnection * connection;
    NSMutableData * data;
}
@end


@implementation AsyncImageView

- (void) loadImageFromURL: (NSURL * ) url {
    if (connection != nil) {
        [connection release];
    }
    if (data != nil) {
        [data release];
    }
    NSURLRequest * request = [NSURLRequest requestWithURL: url
    cachePolicy: NSURLRequestUseProtocolCachePolicy
    timeoutInterval: 60.0];
    connection = [
        [NSURLConnection alloc]
        initWithRequest: request delegate: self];
    //TODO error handling, what if connection is nil?
}

- (void) connection: (NSURLConnection * ) theConnection
didReceiveData: (NSData * ) incrementalData {
    if (data == nil) {
        data = [
            [NSMutableData alloc] initWithCapacity: 2048];
    }[data appendData: incrementalData];
}

- (void) connectionDidFinishLoading: (NSURLConnection * ) theConnection {

    [connection release];
    connection = nil;

    if ([
        [self subviews] count] & gt; 0) {
        [
            [
                [self subviews] objectAtIndex: 0] removeFromSuperview];
    }

    UIImageView * imageView = [
        [
            [UIImageView alloc] initWithImage: [UIImage imageWithData: data]] autorelease];

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight);

    [self addSubview: imageView];
    imageView.frame = self.bounds;
    [imageView setNeedsLayout];
    [self setNeedsLayout];
    [data release];
    data = nil;
}

- (UIImage * ) image {
    UIImageView * iv = [
        [self subviews] objectAtIndex: 0];
    return [iv image];
}

- (void) dealloc {
    [connection cancel];
    [connection release];
    [data release];
    [super dealloc];
}

@end

- (UITableViewCell *)tableView:(UITableView *)tableView
       cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    static NSString *CellIdentifier = @"ImageCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
              initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
              autorelease];
    } else {
        AsyncImageView* oldImage = (AsyncImageView*)
             [cell.contentView viewWithTag:999];
        [oldImage removeFromSuperview];
    }
 
        CGRect frame;
        frame.size.width=75; frame.size.height=75;
        frame.origin.x=0; frame.origin.y=0;
        AsyncImageView* asyncImage = [[[AsyncImageView alloc]
               initWithFrame:frame] autorelease];
        asyncImage.tag = 999;
        NSURL* url = [imageDownload
               thumbnailURLAtIndex:indexPath.row];
        [asyncImage loadImageFromURL:url];
 
        [cell.contentView addSubview:asyncImage];
 
    return cell;
}

分享到:
评论

相关推荐

    中国移动的iOS16 ipcc文件 53

    中国移动的iOS16 ipcc文件 53

    9种移动开发专用的HTML5加载条动画效果.rar

    9种移动开发专用的HTML5加载条动画效果,纯CSS3实现的网页进度条特效,支持HTML5的浏览器就能完美支持,Android手机、iPhone都可以很好的支持,这些进度条每一款的风格都不相同,都带有动画效果,而且没有使用任何的...

    结合UIImageView实现图片的移动和缩放

    因为种种原因,需要在iphone应用中实现图片查看功能,由于iphone屏幕支持多点触摸,于是是想到用“手势”来实现图片的实时缩放和移动。借鉴无所不在的internet网络资料之后,终于实现此一功能,过程如下。为方便大家...

    iOS DevCamp幻灯片分享:社区类iPhone应用开发的技术实践 | 麻麻帮 陈剑飞

    本演讲将结合讲师这些年自学iPhone开发的历程,结合自己多个应用开发实践的案例,与大家分享iOS开发的技术实践,内容将涉及:开源框架的选择,如何搭建程序架构从而写更干净的代码,如何有效使用内存和防止内存泄露...

    HiApp移动开发案例

    今天是给大家介绍一个html5 app开发案例,现在移动开发越来越多,大家选择起来也非常困难,而且跨平台框架在各种平台表现的性能也不一样,可能好点的手机性能要好些,例如iPhone,但是在Android上的表现就不是那么...

    《iPhone开发实战》.(Christopher Allen).pdf

    1.1.2 iphone网络规范4 1.1.3 iphone浏览器规范5 1.1.4 iphone的其他硬件特性5 1.2 iphone在行业中的比较6 1.2.1 物理比较6 1.2.2 具有竞争力的因特网浏览6 1.2.3 移动web标准7 1.2.4 其他创新7 1.3...

    iScroll下拉刷新上滑加载

    这个项目的产生完全是因为移动版webkit浏览器(诸如iPhone,iPad,Android 这些系统上广泛使用) 提供了一种本地化的方式来对一个限定了高度和宽度的元素的内容进行滑动。很不幸的是,这种情况下所有的web应用的页面...

    iPhone开发秘籍.part2.rar

    不负众望,iPhone 可以加载并显示各种格式的媒体。它可以播放音乐和电影,处理图像和Web 页面。 也可以呈现PDF 文档和相册。第7章介绍用多种方法将数据导入或下载到程序中,并使用iPhone 的多点 触摸界面显示这些...

    iPhone开发秘籍.part4.rar

    不负众望,iPhone 可以加载并显示各种格式的媒体。它可以播放音乐和电影,处理图像和Web 页面。 也可以呈现PDF 文档和相册。第7章介绍用多种方法将数据导入或下载到程序中,并使用iPhone 的多点 触摸界面显示这些...

    iPhone开发秘籍.part1.rar

    不负众望,iPhone 可以加载并显示各种格式的媒体。它可以播放音乐和电影,处理图像和Web 页面。 也可以呈现PDF 文档和相册。第7章介绍用多种方法将数据导入或下载到程序中,并使用iPhone 的多点 触摸界面显示这些...

    iPhone开发秘籍

    9.12 秘诀:使用加速度移动屏幕上的对象 243 9.13 小结 246 第10章 连接服务 247 10.1 秘诀:添加自定义设置束 247 10.2 秘诀:使应用程序支持自定义url模式 251 10.3 秘诀:检查网络状态 253 10.3.1 测试...

    iPhone-3D-scanner:在移动设备上进行手绘体素雕刻扫描

    iPhone-3D扫描仪 在移动设备上进行手绘体素雕刻扫描 该应用需要设备,无法在模拟器上运行! 安装说明: 将所有文件放在同一文件夹中 启动PointCloudSample \ PointCloudSample.xcodeproj 按下PLAY按钮,您就完成...

    超多HTML5 制作页面Loading加载的小动画图标集.rar

    超多HTML5 制作页面Loading加载的小动画图标集,个个都很炫哦,每一款都不一样...有圆形、有旋转的、有渐变的,在PC端使用,同时可在移动设备(Android智能手机和iPhone手机端使用),值得收藏哦,前端设计者必备素材。

    Learn iPhone and iPad cocos2d Game Development

     10.2 使用zwoptex处理图片  10.3 tiled地图编辑器  10.3.1 创建新的瓦片地图  10.3.2 设计瓦片地图  10.4 在cocos2d中使用直角瓦片地图  10.4.1 定位被触摸的瓦片  10.4.2 提高性能和可读性  10.4.3 使用...

    iscrolldemo(jb51.net)实现上拉加载.rar

    这个项目的产生完全是因为移动版webkit浏览器(诸如iPhone,iPad,Android 这些系统上广泛使用) 提供了一种本地化的方式来对一个限定了高度和宽度的元素的内容进行滑动。很不幸的是,这种情况下所有的web应用的页面...

Global site tag (gtag.js) - Google Analytics