Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何创建自定义UICollectionViewCell并从URL向其添加图像?_Ios_Objective C_Uiimage_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 如何创建自定义UICollectionViewCell并从URL向其添加图像?

Ios 如何创建自定义UICollectionViewCell并从URL向其添加图像?,ios,objective-c,uiimage,uicollectionview,uicollectionviewcell,Ios,Objective C,Uiimage,Uicollectionview,Uicollectionviewcell,我想将我从flickr获得的图像加载到uicollectionsview 我遵循了分配的指南,但当我不得不这样做时,它们都会崩溃 cell.imageView.image 它基本上说在uicollectionsviewcell的对象中找不到imageView 我也尝试过这样的方法,但没有成功 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath

我想将我从flickr获得的图像加载到uicollectionsview 我遵循了分配的指南,但当我不得不这样做时,它们都会崩溃

    cell.imageView.image
它基本上说在uicollectionsviewcell的对象中找不到imageView

我也尝试过这样的方法,但没有成功

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"flickerCell";

UICollectionViewCell *flickerCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
flickerCell.backgroundColor = [UIColor whiteColor];
UIImageView *recipeImageView = (UIImageView *)[flickerCell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
[flickerCell addSubview:recipeImageView];
return flickerCell;
}
recipieImageView是我在教程中得到的一些东西。 我的单元格在故事板中被命名为FlickCell,photoURLs有33个对象,我可以在代码中查看。所以一切都在那里。另外,PhotoURL是一个NSMutablearray 无论如何。在这个方法中,我的recipeImageView返回nill??? 我有一个单元格。h有图像视图

#import <UIKit/UIKit.h>

@interface Cell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end
再次更新

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"flickerCell";
Cell *flickerCell = (Cell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSLog(@"flickercell = %@", flickerCell);
flickerCell.backgroundColor = [UIColor whiteColor];
flickerCell.imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
return flickerCell;
} 在viewdidload中使用此选项

    [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"flickerCell"];
FlicketCell的NSLOG返回以下内容:

    flickercell = <Cell: 0x9dec2f0; baseClass = UICollectionViewCell; frame = (0 0; 120 115); layer = <CALayer: 0x9dec430>>
flackercell=
它可能会对您有所帮助

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *identifier = @"flickerCell";

        Cell *flickerCell = (Cell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

        flickerCell.backgroundColor = [UIColor whiteColor];
        flickerCell.imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
        return flickerCell;
}

据我所知,您正在使用自定义UICollectionViewCell。但是,在粘贴的方法中,您创建了一个指向UICollectionViewCell的指针,该指针没有内置的UIImageView。因此,这就是为什么您在第一次尝试时会出现错误。 关于第二个-您是否向CollectionView注册了自定义UICollectionViewCell类

======

编辑:与海报长时间聊天后,我们解决了所有问题

  • 第一个是注册的类是UICollectionViewCell,而不是继承的自定义类
  • 第二个问题是该视图实际上位于xib文件中,因此应该使用
    registernb:forCellWithReuseIdentifier:
    而不是
    registerClass:forCellWithReuseIdentifier:
  • 第三个问题是自定义单元是在故事板xib中配置的,而不是它自己的与自定义类连接的xib文件(在本例中为“单元”)
  • 第四个主要问题是,UIImage是使用
    imageNamed:
    创建的,它应该只用于嵌入到项目中的图像。在本例中,创建UIImage的正确方法是:
    flickCell.imageView.image=[UIImage ImageName:[photoURLs objectAtIndex:indexPath.row]
在您的代码中

imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
[photoURLs objectAtIndex:indexath.row]
返回什么

要与
imageNamed:
一起使用,它必须返回一个
NSString
对象,该对象是位于主捆绑包中的资源的名称。它不能与URL一起工作,如果此URL指向远程URL,则更不用说了

无论如何,您不应该使用
imageNamed
。此方法将映像缓存在系统缓存中,这在集合/表视图中不是最佳/必需的。而是使用另一种方法创建
UIImage
对象

问题的解决方案取决于您是否需要从远程服务器加载映像数据,或者这些资源是否位于设备上

在第一种情况下,您需要一种与“占位符图像”结合使用的异步方法,该图像表示在单元格中显示图像时正在加载的图像

在第二种情况下,您可以使用简单的同步方法加载图像(与现有解决方案类似,但例如使用
imageWithContentsOfFile:
),除非图像很大,在这种情况下,您也需要异步方法

注意:您的代码也有一些其他问题-但首先您需要告诉您的图像来自哪里

从设备上的磁盘加载图像的可能(但不是最佳)解决方案:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"flickerCell";
    FlickerCell* flickerCell = (FlickerCell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    flickerCell.backgroundColor = [UIColor whiteColor];
    NSURL* url = [self.photoURLs objectAtIndex:indexPath.row]; // file URL
    flickerCell.imageView.image = [UIImage imageWithContentsOfFile:[url path]];
    return flickerCell;
}

一个更复杂的解决方案是在图像实际显示之前,先在后台线程中加载并解码图像。这样,当它们实际显示时,图像可能已经编码,渲染速度会快得多。这也需要缓存。

点击UICollectionViewCell,可以轻松创建自定义UICollectionViewCell类和自定义动画。 因此,不需要为动画添加任何特定的方法/动作

  • 在情节提要中-添加具有单元标识符、UIImageView和UILabel的UICollectionViewCell
  • 将UIImageView和UILabel属性都指定给情节提要单元
  • 在视图控制器中导入“UviCollectionCell.h”

    在YourViewController.h中 紫外线收集细胞

recipeImageView是我从UIImageView*recipeImageView=(UIImageView*)[FlickCell viewWithTag:100]创建的;难道你不认为你可以“不首先声明就声明它吗?”@KennyVB你实际上想访问
imageview
,它是在继承自
UICollectionCell
Cell
类中声明的。我刚刚演示了如何访问它。!使用未声明的标识符“recipeImageView”,这就是我尝试您的代码时所说的。。。正如我所说,recipeImageView是从UIImageView*recipeImageView=(UIImageView*)[FlickCell viewWithTag:100]创建的;这是因为我不能不先做就直接使用它。还是我错了?@KennyVB明白了。我没看到。谢谢你的批改。我也更新了。嗯,刚刚试过你的更新代码。它进行验证,但由于未捕获的异常“NSInvalidArgumentException”,导致此***终止应用程序崩溃,原因:'-[UICollectionViewCell imageView]:未识别的选择器发送到实例0xaadce70'不确定您在说什么,对此仍有点陌生。但让我想想。我已经导入了我的Cell.h,它是一个uicollectionsviewcell,其中我有一个imageView。怎么还不工作?然后用UIImageView指向UICollectionViewCell?好的。在viewDidLoad()方法中,您必须执行如下操作:
[myCollectionView注册表类:[MyCustomCell类]forCellWithReuseIdentifier:@“MyCustomCellIdentifier”]
[myCollectionView注册表项nb:[UINib nibNamed:@“MyCustomCell”forCellWithReuseIdentifier:@“MyCustomCellIdentifier”];
取决于您是仅从代码还是使用xib文件设置自定义单元格。然后在cellForItem中……您可以执行以下操作:
MyCustomCell*flickerCell=[collectio]
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"flickerCell";
    FlickerCell* flickerCell = (FlickerCell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    flickerCell.backgroundColor = [UIColor whiteColor];
    NSURL* url = [self.photoURLs objectAtIndex:indexPath.row]; // file URL
    flickerCell.imageView.image = [UIImage imageWithContentsOfFile:[url path]];
    return flickerCell;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UviCollectionCell *cell = (UviCollectionCell *)  [cv dequeueReusableCellWithReuseIdentifier:@"collection_cell" forIndexPath:indexPath];
    [cell.cellImageView setBackgroundColor: [UIColor randomColor]];
    // You can add your image here.
    cell.cellImageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
    [cell.cellTitle setText:[NSString stringWithFormat:@"S %ld - R %ld",(long)indexPath.section,(long)indexPath.row]];

    //cell.backgroundColor = [UIColor randomColor];
    return cell;
}  
    #import <UIKit/UIKit.h>

    @interface UviCollectionCell : UICollectionViewCell<UIGestureRecognizerDelegate>

    @property (strong, nonatomic) IBOutlet UIImageView *cellImageView;
    @property (strong, nonatomic) IBOutlet UILabel *cellTitle;

    @end
    #import "UviCollectionCell.h"

    @implementation UviCollectionCell

    // Initialize the collectiion cell based on the frame and add the tap gesture recognizer for custom animation
    - (id)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];

        // Custom animation while tap the CollectionCell view.
        UITapGestureRecognizer *tapReconizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedCellAnimation)];
        [tapReconizer setNumberOfTouchesRequired : 1];
        [tapReconizer setDelegate:self];
        [self addGestureRecognizer:tapReconizer];

        return self;
    }

    // Initialize the collection cell based on the nscoder and add the tap gesture recognizer for custom animation
    -(id) initWithCoder:(NSCoder *)aDecoder {
         self = [super initWithCoder:aDecoder];
        // Custom animation while tap the CollectionCell view.
        UITapGestureRecognizer *tapReconizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedCellAnimation)];
        [tapReconizer setNumberOfTouchesRequired : 1];
        [tapReconizer setDelegate:self];
        [self addGestureRecognizer:tapReconizer];

         return self;
    }

    // Initialize the collectiion cell and add the tap gesture recognizer for custom animation
    -(id) init {
        self = [super init];

        // Custom animation while tap the CollectionCell view.
        UITapGestureRecognizer *tapReconizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedCellAnimation)];
        [tapReconizer setNumberOfTouchesRequired : 1];
        [tapReconizer setDelegate:self];
        [self addGestureRecognizer:tapReconizer];

        return self;
    }

    // Customize the animation while tap the UICollectionViewCell with custom animation duration.
    - (void)selectedCellAnimation {
        [UIView animateWithDuration:0.4 delay:0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
            CALayer *layer = self.layer;
            CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
            rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 25.0f * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
            layer.transform = rotationAndPerspectiveTransform;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionCurveEaseOut) animations:^{
                CALayer *layer = self.layer;
                CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
                rotationAndPerspectiveTransform.m24 = 0;
                rotationAndPerspectiveTransform =CATransform3DRotate(rotationAndPerspectiveTransform, 0.0f * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
                layer.transform = rotationAndPerspectiveTransform;
            } completion:nil];
        }];
    }

    @end