我一直在尝试制作一个图像查看器,但我真的没有什么不起作用。这是一张你应该得到的照片!
这是 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
在这个例子中,图像没有加载,我不明白为什么?我一直使用这些从互联网加载图像的类,但我也遇到了内存问题,我也不知道如何解决它。例如,当滚动图像时出现异常 -收到内存警告。Level=1 & 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 全部!