0

所以,对于 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];

}
4

2 回答 2

1

将两个 UIButton 在您的视图中放置在 .xib 中(如果您使用的是 IB)。

在视图控制器 .h 中创建两个 IBAction:

-(IBAction)nextClicked:(id)sender;
-(IBAction)prevClicked:(id)sender;

和.m:

-(IBAction)nextClicked:(id)sender
{
  //do stuff
  imageView.image = whatever;
}

-(IBAction)prevClicked:(id)sender
{
  //do stuff
  imageView.image = whatever;
}

接下来,通过 IB 将这些操作链接到按钮的“内部修饰”事件。这将使这些事件触发。

于 2011-05-30T22:45:58.270 回答
0

将图像放在一个数组中,并将当前图像的索引放在一个变量中

NSInteger *currentPhotoIndex;

- (IBAction)nextTouch
{
    [self showPhoto:1]; // number to add to current, so 1 for next, -1 for previous
}

- (IBAction)previousTouch
{
    [self showPhoto:-1];
}


// Actual "Logic"

- (void)showPhoto:(NSInteger)numberToAdd
{
    NSArray *arrayOfImages = allTheImagesYouWantToShow
    UIImageView.image = [arrayOfImages objectAtIndex:currentPhotoIndex + numberToAdd];
    // Adding one or subtracting one from the current image's index will show the photo
    immediately before or after it. With this method, you can also skip around to any
    number you want.
}

我认为应该这样做。

- 编辑:

currentPhotoIndex 应该像你所做的那样在你的头文件中定义。它应该设置为您显示的第一张图像的数组中的位置。现在,让我们假设它是第一个。

第一次显示图像时,请执行以下操作:

currentPhotoIndex = [arrayOfImages indexOfObject:thePhotoI'mShowingRightNow];

此外,很好的收获。为了防止 NSInteger 超出数组的范围,您需要这样的东西:

- (void)showPhoto:(NSInteger)numberToAdd
{
    if (currentPhotoIndex > 0 && < arrayOfImages.count) // Makes sure that the int is within the array
    {
        // Show new image
    }
}
于 2011-05-30T23:52:25.893 回答