简体   繁体   English

IOS如何从cell.contentview.subview [0]的collectionView单元格didSelectItemAtIndexPath方法获取图像

[英]ios how to get image from collectionView cell didSelectItemAtIndexPath method from cell.contentview.subview[0]

I have download this Code form this link http://code4app.net/ios/FullScreenImageViewController/519cbf9a6803faaf72000000 I have add image in cell like this 我已经通过此链接下载了此代码http://code4app.net/ios/FullScreenImageViewController/519cbf9a6803faaf72000000我在像这样的单元格中添加了图像

 NSString *imageURL = [NSString stringWithFormat: @"%@",[[ary_UserPicture objectAtIndex:indexPath.row]valueForKey:@"StorePicturePath"]];
        if(imageURL.length != 0)
        {
            AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(15, 5, 100, 100)];
            [async loadImageFromURL:[NSURL URLWithString:imageURL]];
            async.backgroundColor=[UIColor whiteColor];
            [cell.contentView addSubview:async];
        }

After that i want to zoom image according this demo link , but i am put this code in didSelectItemAtIndexPath this method my app crash on vc.liftedImageView = cell.contentView.subviews[0]; 之后,我想根据此演示链接缩放图像,但是我将此代码放入didSelectItemAtIndexPath中,此方法使我的应用程序在vc.liftedImageView = cell.contentView.subviews [0]上崩溃 on this code please give me solution how to get image for Zoom This is my code 关于此代码,请给我解决方案如何获取缩放图像这是我的代码

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
    NSLog(@"%@",cell.contentView);
    NSLog(@"%@",cell.contentView.subviews[0]);
    vc.liftedImageView = cell.contentView.subviews[0];
    [self presentViewController:vc animated:YES completion:nil];
}

**The problem is How to get image in didSelectItemAtIndexPath method i have using Async image ** **问题是如何使用异步图像在didSelectItemAtIndexPath方法中获取图像**

please share your valuable knowledge , Is there any other demo link for zoom image from collection view please share with me Thankyou . 请分享您的宝贵知识,从收藏夹看缩放图像是否还有其他演示链接请与我分享谢谢。

Here issue is because of below code. 这里的问题是由于下面的代码。

if (imageURL.length != 0) {
    // Stuff to download image
}

From above condition we can see that if imageURL varialbe length is greater than 0 than only images are added in cell. 从以上条件可以看出,如果imageURL varialbe长度大于0,则仅在单元格中添加图像。

So there is a case in that you don't have valid url or url is nil (In any means condition is false). 因此,有一种情况是您没有有效的url或url为nil(无论如何,条件为false)。 And it will be not added images to cell. 并且不会将图像添加到单元格中。

vc.liftedImageView = cell.contentView.subviews[0];

Now above line will definitely crash your application because you don't have any sub view (no images) in cell. 现在上面的那行肯定会导致应用程序崩溃,因为您的单元格中没有任何子视图(没有图像)。

Suggestion: 建议:
Why don't you create an imageView in cell. 为什么不在单元格中创建imageView? Add UIImageView variable in your cell. 在您的单元格中添加UIImageView变量。 So that when you'll get image downloaded you just set image to that imageView. 这样,当您下载图像时,只需将图像设置为该imageView即可。 Like below. 像下面。

NSString *imageURL = [NSString stringWithFormat: @"%@",[[ary_UserPicture objectAtIndex:indexPath.row]valueForKey:@"StorePicturePath"]];
if(imageURL.length != 0)
{
     AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(15, 5, 100, 100)];
     [async loadImageFromURL:[NSURL URLWithString:imageURL]];
     async.backgroundColor=[UIColor whiteColor];
     // [cell.contentView addSubview:async]; --> Your previous implementation         
     // Instead of adding it assign it to your cell's imageView object

     cell.imageView = async;
}

Rewrite code for didSelectItemAtIndexPath like below: 重写didSelectItemAtIndexPath代码,如下所示:

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    @try {


        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

        // A safe check before going to access sibview.
        if ([cell.contentView.subviews count] > 0) {

            GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
            NSLog(@"%@",cell.contentView);

            //NSLog(@"%@",cell.contentView.subviews[0]);
            //vc.liftedImageView = cell.contentView.subviews[0];

            NSLog(@"%@",cell.imageView.image);
            vc.liftedImageView = cell.imageView.image;

            [self presentViewController:vc animated:YES completion:nil];
        }

    }
    @catch (NSException *exception) {
        NSLog(@"Exception :%@",exception.debugDescription);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS为什么不能从UITapGestureRecognizer调用方法collectionView:didSelectItemAtIndexPath :? - iOS Why can't the method collectionView:didSelectItemAtIndexPath: be called from UITapGestureRecognizer? 如何在uicollectionview中获取当前的cell.contentview。 如何在uicollectionview单元格中添加子视图 - how to get current cell.contentview in uicollectionview. how to add subview in uicollectionview cell 将子视图添加到cell.contentView - Adding subview to cell.contentView 如何通过子视图ios返回的值更改字幕单元格的值 - How to change value of subtitle cell by value returned from subview ios 如果滚动,swift collectionview单元格didSelectItemAtIndexPath显示错误的单元格 - swift collectionview cell didSelectItemAtIndexPath shows wrong cell if you scroll 如何从collectionview中单击的单元格中获取信息 - how to get the information from the cell which is being clicked in collectionview Swift如何从CollectionView中的准备准备中获取单元格 - Swift how to get cell from prepare for segue in collectionView 从 TableView 中的 CollectionView 单元格获取信息 - Get Info from CollectionView Cell Within TableView 标头顶部的CollectionView单元子视图 - CollectionView Cell subview on top of header 将子视图添加到collectionview单元格jsqmessagesviewcontroller - add subview to collectionview cell jsqmessagesviewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM