【问题标题】:IOS: change UICollectionView Cell size according to the image sizeIOS:根据图片大小改变UICollectionView Cell大小
【发布时间】:2013-08-15 09:16:47
【问题描述】:

我想生成一个图片库,而不更改图片的纵横比。图像有不同的尺寸。我正在使用UICollectionView 并将UIImageView 定位在单元格内。我使用下面的代码来获取图像大小。但它总是返回大小 0。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

NSString* url = [selectedImages objectAtIndex:indexPath.row];

UIImageView *thumb;
ALAssetsLibraryAccessFailureBlock failureBlock  = ^(NSError *myError) {
    NSLog(@"failed loading asset");
};
ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myAsset) {
    [thumb setImage:[UIImage imageWithCGImage:[myAsset aspectRatioThumbnail]]];
};
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];

[lib assetForURL:[NSURL URLWithString:url] resultBlock:resultBlock failureBlock:failureBlock];
NSLog(@"%f,%f",thumb.image.size.height,thumb.image.size.width);
return thumb.image.size;   
}

【问题讨论】:

    标签: iphone ios uicollectionview


    【解决方案1】:

    你总是得到图像大小返回大小 0 因为你的UIImageview () 在^{} block 中获取图像并且它没有在mainthread 上执行, 所以,你必须在sizeForItemAtIndexPath这个方法之前计算图像大小或者

    你可以这样尝试,但我想这不是正确的方法,

    ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myAsset) {
        [thumb setImage:[UIImage imageWithCGImage:[myAsset aspectRatioThumbnail]]];
    return thumb.image.size;   
    
    };
    

    【讨论】:

    • 查看http://omegadelta.net/2011/05/10/how-to-wait-for-ios-methods-with-completion-blocks-to-finish/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2018-02-03
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多