-1

我不明白我在代码中的哪个位置写了图像名称以及出现的顺序。这是我的代码

// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
    NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

我有标题为“image0.jpeg, image1.jpg”的图片。如何将其插入我的代码并以某种方式订购它们?

4

3 回答 3

0

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

将 %d 用于有符号整数

和 %u 表示无符号

于 2009-08-06T06:54:19.060 回答
0

您的代码片段已经在做您想做的事 - 至少部分如此。

如果您有一个编号为 0 的图像,那么您需要从i = 0 instea of​​ i = 1开始循环,并适当地调整约束:

for (NSUInteger i = 0; i < kNumImages; i++) {
    NSString *imageName = [NSString stringWithFormat:@"image%u.jpg", i];

    // create the image and insert into imageview
    // add imageview to the containing view
}

顺序非常简单,因为图像将添加 0、1、2.. 等等

于 2009-08-06T06:54:29.280 回答
0

With the code from your first post, you create multiple (kNumImages, to be more specific) ImageViews, and load in them JPG files from your project directory, called "imageN.jpg", where N is integer between 1 and kNumImages.

To display these newly created views in your UIScrollView, you have to add them to it as subviews.

Something like

for (int i = 0; i < pageCount; i++) {
    UIView *view = [pageViews objectAtIndex:i];
    if (!view.superview)
        [scrollView addSubview:view];
    view.frame = CGRectMake(pageSize.width * i, 0, pageSize.width, pageSize.height);
}

The most straightforward way to do this is in UIScrollView's UIViewController. You may want to add them in some sort of collection (it's likely that the collection retains them, so don't forget to release the views, when you add them).

As you get more comfortable with your application, you may want to lazy-load the views or use simple UIViewControllers for the different images.

In iPhone OS 3.0 and above, there are significant improvements in UIScrollView, and you can find excellent code samples in the ScrollViewSuite tutorial project on ADC's iPhone section.

于 2009-08-22T12:34:57.673 回答