照片浏览器iPad&苹果手机

时间:2011-06-10 17:09:54

标签: ios uiscrollview nsoperationqueue photoviewer

我一直在尝试制作一个图像浏览器,但实际上我并不是什么都不行。 这是你应该得到的照片!

enter image description here

这是UIScrollView。 UIImage添加到UIScrollView。当用户滚动 - 图像必须下载到UIImageView。我想我们需要在一个单独的线程中下载图像。为此我正在使用 - NSOperationqueue和NSInvocationOperation。

这是我的代码。

代表:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    MyThumbnailsBook * myThumbnailsBook = [[MyThumbnailsBook alloc] initWithFrame:CGRectMake(0, 20, 768, 1004)];
    [myThumbnailsBook createThumbnails];
    [myThumbnailsBook setContentSize:CGSizeMake(768, 1004 * 10)];
    [myThumbnailsBook setBackgroundColor:[UIColor blackColor]];
    [self.window addSubview:myThumbnailsBook];
    [self.window makeKeyAndVisible];
    return YES;
}

在类中继承UIScrollView MyThumbnailsBook.m

#define COUNT 100

#import "MyThumbnailsBook.h"
#import "MyImageDownload.h"

@implementation MyThumbnailsBook


#pragma mark -
#pragma mark Initialization & Create Thumbnails

- (id)initWithFrame:(CGRect)frame{

    if((self = [super initWithFrame:frame])){

        self.delegate = self;

    }

    return self;
}

- (void)createThumbnails{

    float point_x = 20;
    float point_y = 20;

    for(NSInteger i = 0; i < COUNT; i++){

        if(i%3==0 && i != 0){
            point_x = 20;
            point_y += 220;
        }

        //  Create new image view.
        NSURL * url = [[NSURL alloc] initWithString:@"http://storage.casinotv.com/videos/SOYCL/pages/P68.jpg"];        
        MyImageDownload * imageDownload = [[MyImageDownload alloc] initWithImageURL:url];
        [imageDownload setFrame:CGRectMake(point_x, point_y, 200, 200)];
        [self addSubview:imageDownload];
        [imageDownload release];
        [url release];

        point_x += 220;

    }


}

#pragma mark -
#pragma mark Scroll View Protocol

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{


}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    // If scroll view no decelerating.

    if(!decelerate){
        [self asyncImageDownload];
    }

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 

    [self asyncImageDownload];

}


#pragma mark -
#pragma mark Download Image

//  If scroll stopped - download image to thumbnails.

- (void)asyncImageDownload{


}


@end

在类中继承UIScrollView MyImageDownload.m

#import "MyImageDownload.h"


@implementation MyImageDownload

//  This is init method. When class init - we add new operation for download image.

- (id)initWithImageURL:(NSURL*)url{

    if((self == [super init])){

        [self setBackgroundColor:[UIColor yellowColor]];        

        NSArray * params = [NSArray arrayWithObjects:url, nil];
        queue = [[NSOperationQueue alloc] init];
        [queue setMaxConcurrentOperationCount:1]; 
        NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(asyncDownload:) object:params];           
        [queue addOperation:operation];        
        [params release];             

    }

    return self;
}


//  Download image to data and after to self (UIImageView).

- (void)asyncDownload:(NSArray *)params{


    NSData * data = [NSData dataWithContentsOfURL:[params objectAtIndex:0]]; //  Get image data with url.

    if(data) [self setImage:[UIImage imageWithData:data]];

    [data release]; 

}

@end

在这个例子中,图像没有加载,我不明白为什么? 我一直使用这些从互联网上加载图像的类,但我也遇到了内存问题,我也不知道如何解决它。例如,当滚动图像时,我得到异常 - 收到内存警告。等级= 1&amp; 2 和应用程序崩溃后。我不知道该怎么做。我理解你需要随着时间的推移单独下载图像并删除不可见的对象,但我发现我需要算法。

例如 - 当我去scrollDidScroll方法时,我得到所有对象,当我看不到它们并删除图像时:

NSArray *views = [self subviews];
    for (UIImageView *v in views) {
        if(v.frame.origin.y >= self.contentOffset.y && v.frame.origin.y <= self.contentOffset.y + self.frame.size.height){

            //  If image of imageview is equal nil - call new operation
            if(v.image == nil){             
                [self requestImageForIndexPath:[NSNumber numberWithInt:v.tag]];                               
            }
            else
            {
                v.image = nil;
            }

        }
    }
  • self - 这是类继承UIScrollView。

我很困惑,并在解决此问题时寻求帮助。 TNX全部!

1 个答案:

答案 0 :(得分:0)

The Three20 libraryTTPhotoViewController

的形式包含此功能