【问题标题】:Enlarging image when tapped ios点击ios时放大图像
【发布时间】:2015-07-08 18:10:24
【问题描述】:

我有一个实现水平图库的应用。在我的故事板中,当点击作业(表格视图中的单元格)时,它将重定向并显示作业详细信息。在我的工作细节场景中:我有索引 0:用于公司徽标和名称、薪水和职位。在索引 1 中:我有水平画廊 - 我以编程方式创建了一个滚动视图并在滚动视图内创建了一个图像视图。 以下是我创建滚动视图和图像视图的方法:

else if (indexPath.section == 1)
{
    [self getImages];
    [cell.contentView addSubview:self.scrollView];

    return cell;
}

- (void)getImages
{
    mutableURLstorage = [[NSMutableArray alloc] init];
    imagesArray = [[NSMutableArray alloc]init];
    NSInteger noOfImages = (unsigned long)[mutableStorage count];
    NSString* strURL;

    //store image_paths (company and branch) in a mutable array
    for (int i = 0; i < noOfImages; i++)
    {   
        NSDateFormatter *df=[[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSDate *date = [NSDate date];
        NSDate *oneMinLater = [date dateByAddingTimeInterval:1444];
        NSString *hmac_body =[NSString stringWithFormat:@"%@\n%lld\n%@",METHOD,[@(floor([oneMinLater timeIntervalSince1970])) longLongValue],CONTAINER_PATH] ;

        // below get different string with shaA diagest
        const char *cKey2 = [KEY cStringUsingEncoding:NSASCIIStringEncoding];
        const char *cData2 = [hmac_body cStringUsingEncoding:NSASCIIStringEncoding];
    unsigned char cHMAC2[CC_SHA1_DIGEST_LENGTH];
        CCHmac(kCCHmacAlgSHA1, cKey2, strlen(cKey2), cData2, strlen(cData2), cHMAC2);
        NSData *HMAC2 = [[NSData alloc] initWithBytes:cHMAC2 length:sizeof(cHMAC2)];
        NSString *HMACStr = [[HMAC2 description] stringByReplacingOccurrencesOfString:@"<" withString:@""];
        HMACStr = [HMACStr stringByReplacingOccurrencesOfString:@">" withString:@""];
        HMACStr = [HMACStr stringByReplacingOccurrencesOfString:@" " withString:@""];
        strURL = [NSString stringWithFormat:@"%@%@?temp_url_sig=%@&temp_url_expires=%lld", URLstorage, CONTAINER_PATH, HMACStr, [@(floor([oneMinLater timeIntervalSince1970])) longLongValue]];

        [mutableURLstorage addObject:strURL];
        [imagesArray addObject:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]]];
}

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGSize pageSize = CGSizeMake(ITEM_WIDTH, self.scrollView.frame.size.height);
    _pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, screenRect.size.height-30, 320, 36)];
    _pgCtr.backgroundColor=[UIColor grayColor];

    int numberofPage=ceil((float)[imagesArray count]/3.5);
    _pgCtr.numberOfPages= numberofPage;
    self.scrollView.contentSize = CGSizeMake(320*numberofPage, pageSize.height);

    int imgCurrentX = 10;
    for (UIImageView* image in imagesArray) {
        @autoreleasepool {
            imageview = [[UIImageView alloc] initWithFrame:CGRectMake(imgCurrentX, 0, 70, 70)];
            imageview.image = (UIImage*)image;
            [imageview setUserInteractionEnabled:YES];
            [self.scrollView addSubview:imageview];
            imgCurrentX = imgCurrentX+80;
        }
    }
}

我可以显示水平画廊,但图像都是缩略图。 现在我在 viewDidLoad 中实现了这段代码,以确定是否点击了 imageview:

[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)]];

在我的 singleTapAction 方法中,我使用LeveyPopListView 来显示放大的图片。

- (void)singleTapAction:(UIGestureRecognizer *)singleTap_
{
    [self createLeveyPopList];
}

- (void)createLeveyPopList
{
    NSString *jobs_name = @"JOBSD";

    if(self.lplv.delegate != nil)
        return;

    self.lplv = [[LeveyPopListView alloc] initWithTitle:@"The following jobs are located here:" options:imagesArray jobName:jobs_name handler:^(NSInteger anIndex) {
}];
    self.lplv.delegate = self;
    [self.lplv showInView:self.view animated:YES];
}

我在弹出窗口中所做的是重新显示原始图片库(工作详细信息场景中的图片库),但图片被放大了。我的应用程序成功实现了水平图库。现在我的问题是,例如点击图像 2,我需要先显示图像 2 而不是先显示图像 1。 我希望有人可以帮助我。谢谢!

【问题讨论】:

    标签: ios objective-c image-gallery


    【解决方案1】:

    添加一个方法来更改包含图像的 ImageView 的大小,如下所示:

    - (IBAction)buttonTapped:(id)sender {
     // Use this Sender property to your advantage
       [yourImageViewName setFrame:CGRectMake(0, 0, 100, 100)];
    }
    

    当然,可以根据需要调整坐标。您还可以在 IB 中的图像视图设置中进行调整,以确保您的图片在放大时不会变形。

    现在,您还可以为图像添加标签,并根据您按下的标签放大图像视图。如果您想改用那条路线,请告诉我,我将发布代码。希望我能帮上忙!

    【讨论】:

    • 嘿,谢谢你的回复。就我而言,我存储在 imagesArray 中的图像来自服务器。现在图像的数量取决于从服务器返回给我的图像数量。现在我的问题是,如果我要使用“标签”,当我从服务器获取它们时,我将如何标记每个图像?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    相关资源
    最近更新 更多