所以,对于 iPhone 开发和学习来说,它还是个新手。我知道我想要什么,但不知道从哪里开始。在这个项目之前,我真的只处理过 tableView 应用程序,所以我对 imageView 和自定义操作的实际知识非常缺乏!
我想让 UIImageView 加载图像(使用由 fetchedResultsController 填充的数组,因为图像将存储在 CoreData DB 中)。
我想要一个带有“下一个”动作的 UIButton 和一个带有“上一个”动作的 UIButton,它们将控制前面提到的 imageViews 显示的图像。
到目前为止,我已经完成了 DB 映射和界面设计,但未能找到这些问题的答案(我相信这对那些知道的人来说是相当直接的)。
任何建议或示例代码将不胜感激。
去酒石
@shawnwall 下面的代码片段...
-(IBAction)nextImage {
index++;
int imageCount=31;
if (index<=imageCount) {
NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
[picture setImage: [UIImage imageNamed: imageName]];
}
}
在 viewDidLoad 中播种的可变数组:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Images" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"topImage" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
NSMutableArray *mutableFetchResultsT = [[managedObjectContext executeFetchRequest:request error:&error]mutableCopy];
if (mutableFetchResultsT == nil) {
}
[self setTopImageArray:mutableFetchResultsT];
NSLog(@"%i" , [topImageArray count]);
在此处将图像添加到数组中:
Images *imageS = (Images *)[NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:managedObjectContext];
imageS.topImage = selectedImage;
[topImageArray insertObject:imageS.topImage atIndex:0];
NSLog(@"%i" , [topImageArray count]);
……
相同的方法,但在创建缩略图后,我调用:
[self updateTopIV];
这是:
-(void)updateTopIV
{
topIV.image = [topImageArray objectAtIndex:0];
}
然后我有 2 个操作按钮(下一个/上一个):
- (IBAction)nextTouch
{
[self showPhoto:1];
}
- (IBAction)previousTouch
{
[self showPhoto:-1];
}
- (void)showPhoto:(NSInteger)numberToAdd
{
topIV.image = [topImageArray objectAtIndex:currentPhotoIndex + numberToAdd];
}