我正在努力寻找一种在后台运行此方法的方法,以免中断UI。
我试图将UIImageView * ImgArray设置为NSArray或NSMutableArray,可以某种方式与GCD一起使用或以某种方式返回UIScrollView或UIImageArray。我一直在走向死胡同。
修改
我在
之前使用了错误的方法 -(void)getSlideshow{
//This populates string array from a URL
//[self populateStringArray];
NSString *prefix = @"http://newsongbismarck.com/images/announcements/";
NSString *suffix = @".jpg";
//NSUInteger count = [imageStringArray count];
//get this from the website for now
int number = (int)[imageStringArray count];
int PageCount = number;
//Setup scroll view
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 265, 320, 200)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount * Scroller.bounds.size.width, Scroller.bounds.size.height);
//Setup Each View Size
CGRect ViewSize = Scroller.bounds;
int x = PageCount;
int numb = 0;
UIImageView *ImgArray[PageCount];
//This loop adds the Images to a UIImageView within a UIScrollView
while(x!=0){
ImgArray[numb] = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgArray[numb] setImage:[self getSlideshowImages:[[prefix stringByAppendingString:imageStringArray[numb]]stringByAppendingString:suffix]]];
//I haven't been able to make this work with dispatch_async main thread
[Scroller addSubview:ImgArray[numb]];
//Offset View Size
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
x--;
numb++;
}
CGRect newViewSize = Scroller.bounds;
int NumberOfImages = 21;
int i;
for(i=0;i<NumberOfImages;i++){
if(i == 0){
//Setup first picture
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:newViewSize];
NSString *FilePath = [NSString stringWithFormat:@"Page_%d.png", i];
[ImgView setImage:[UIImage imageNamed:FilePath]];
[Scroller addSubview:ImgView];
}else{
//Setup the rest of the pictures
newViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:newViewSize];
NSString *FilePath = [NSString stringWithFormat:@"Page_%d.png", i];
[ImgView setImage:[UIImage imageNamed:FilePath]];
[Scroller addSubview:ImgView];
}
}
//This adds the Scroller
[self.view addSubview:Scroller];
}
EDIT2:
这是我对GCD的尝试,我不知道在哪里添加或设置滚动条视图或UIImageView ...... AFNetworking看起来很有希望。
代码仍然非常混乱。
-(void)showSlideShow{
[activity startAnimating];
[self populateStringArray];
NSString *URLString = @"http://newsongbismarck.com";
NSURL *url = [NSURL URLWithString:URLString];
if(url){
dispatch_queue_t slideQueue = dispatch_queue_create("Slide Show Queue",NULL);
dispatch_async(slideQueue, ^{
// Perform long running process
//[self populateStringArray];
NSLog(@"Start background");
NSString *prefix = @"http://newsongbismarck.com/images/announcements/";
NSString *suffix = @".jpg";
//NSUInteger count = [imageStringArray count];
//get this from the website for now
int number = (int)[imageStringArray count];
int PageCount = number;
//Setup scroll view
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 265, 320, 200)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount * Scroller.bounds.size.width, Scroller.bounds.size.height);
//Setup Each View Size
CGRect ViewSize = Scroller.bounds;
int x = PageCount;
int numb = 0;
UIImageView *ImgArray[PageCount];
while(x!=0){
ImgArray[numb] = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgArray[numb] setImage:[self getSlideshowImages:[[prefix stringByAppendingString:imageStringArray[numb]]stringByAppendingString:suffix]]];
//[Scroller addSubview:ImgArray[numb]];
//Offset View Size
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
x--;
numb++;
}
CGRect newViewSize = Scroller.bounds;
int NumberOfImages = 21;
int i;
for(i=0;i<NumberOfImages;i++){
if(i == 0){
//Setup first picture
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:newViewSize];
NSString *FilePath = [NSString stringWithFormat:@"Page_%d.png", i];
[ImgView setImage:[UIImage imageNamed:FilePath]];
[Scroller addSubview:ImgView];
}else{
//Setup the rest of the pictures
newViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:newViewSize];
NSString *FilePath = [NSString stringWithFormat:@"Page_%d.png", i];
[ImgView setImage:[UIImage imageNamed:FilePath]];
[Scroller addSubview:ImgView];
}
}
NSLog(@"End of background tasks");
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
NSLog(@"Start main thread");
int x = PageCount;
int numb = 0;
UIImageView *ImgArray[PageCount];
while(x!=0){
[Scroller addSubview:ImgArray[numb]];
x--;
numb++;
}
[self.view addSubview:Scroller];
[activity stopAnimating];
});
});
}else{
loadLabel.text = @"Error - could not load images";
[activity stopAnimating];
}
}
答案 0 :(得分:0)
不是你要问的是不可能的,但是你考虑过一种更简单的方法吗?
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
[self.view addSubview:imageView];
imageView.animationImages = @[image1, image2, image3];
imageView.animationDuration = imageView.animationImages.count * 1.0f; // 1 second per image
imageView.animationRepeatCount = 0; // 0 means loop forever
[imageView startAnimating];