0

ですから、iPhone開発者にとってはかなり新しいものであり、常に学習しています。私は自分が何をしたいのかは知っていますが、どこから始めればよいのかよくわかりません。このプロジェクトの前は、実際にはtableViewアプリしか扱っていなかったので、imageViewとカスタムアクションに関する実際的な知識が非常に不足しています。

UIImageViewに画像をロードしたい(画像はCoreData DBに保存されるため、fetchedResultsControllerによって入力された配列を使用)。

前述のimageViewsの表示画像を制御するアクション「next」のUIButtonとアクション「previous」のUIButtonが必要です。

これまでのところ、DBのマッピングとインターフェイスの設計は行っていますが、これらの点に対するこの答えを見つけることができていません(知っている人にとってはかなり簡単だと確信しています)。

アドバイスやサンプルコードをいただければ幸いです。

DetartrateD

以下の@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

ビュー内の2つのUIButtonを.xibに配置します(IBを使用している場合)。

ビューコントローラに2つのIBActionを作成します。h:

-(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 に答える