【问题标题】:One call works but not the other (same) w AssetLibrary / UIImage一个电话有效,但另一个电话无效(相同) w AssetLibrary / UIImage
【发布时间】:2012-11-16 16:59:58
【问题描述】:

在我的“newPlayerVC”中有一个 UIIMagePickerController,它将缩略图(在我将其调整为 100x100 像素之后)保存到 AssetLibrary,没有任何问题。

在保存图像后,我再次调用 AssetLibrary 以获取刚刚保存的图像以显示给用户(并检查它是否正常工作)。那个调用看起来像这样;

// Display new thumbnail via AssetLIbrary call
    __block UIImage *_image = nil;
    [assetLib assetForURL:(playerImageURL)
              resultBlock:^(ALAsset *asset) {
                  _image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
                  NSLog(@"Got the asset thumbnail, %@",_image);
                  [playerImageButton setImage:_image forState:UIControlStateNormal];
              }

             failureBlock:^(NSError *error) {
                 NSLog(@"Asset (image) fetch failed because %@",error);
             }];

到目前为止一切正常。 终端确认日志:“p.playerImage = assets-library://asset/asset.JPG?id=A79242D5-BC91-490B-BC37-0A9F529675EE&ext=JPG”和“获取资产缩略图,UIImage:0xcb5b8a0” - 所以我知道它正在从资源库中获取图像。

但是然后我尝试从我的“playerTableVC”中进行一个非常类似的调用,以使用之前保存的图像将 tableview 填充到我的自定义单元格中的 UIView - 它只是不起作用(自定义单元格没问题并且可以使用静态图像)。我也没有从 Xcode 收到任何错误或警告。从函数“- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath";

// Set up Asset library
ALAssetsLibrary *assetLib = [[ALAssetsLibrary alloc]init];

__block UIImage *_image = nil;
[assetLib assetForURL:[NSURL URLWithString:p.playerImage]
          resultBlock:^(ALAsset *asset) {
              _image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
              NSLog(@"Got the asset thumbnail, %@", _image);
          }

 failureBlock:^(NSError *error) {
     NSLog(@"Asset fetch failed: %@",error);
 }];

// If no cell, create new
if (cell == nil) {
    cell = [[CellPlayer alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

// If cell present, assign values
NSLog(@"p.playerImage = %@", p.playerImage);  // Check = ok

// Check if imageurl is empty = default image else the user choosen image
if ([p.playerImage isEqualToString:@""]) {
    cell.cellPlayerIcon.image = [UIImage imageNamed:@"defaultImageFrame_114x114"];
    // Add sortingorder ("player position") to tableview
    cell.cellPlayerStarIcon.text = [NSString stringWithFormat:@"%i", (indexPath.row +1)];
} else {
    cell.cellPlayerIcon.image = _image;  // = NO IMAGE IN CELL?
    // Add sortingorder ("player position") to tableview
    cell.cellPlayerStarIcon.text = [NSString stringWithFormat:@"%i.", (indexPath.row +1)];
}

cell.cellPlayerName.text = p.playerName;
cell.cellPlayerTime.text = p.playerTime;
cell.cellPlayerTimestamp.text = [NSString stringWithFormat:@"%@", p.playerTimeStamp];  // @"2012-10-01 @ 8:06";

return cell;

}

我相信我在填充上面的单元格时 UIIMage 的设置有问题。其他一切都可以正常工作,也可以使用静态 UIImage。尝试了很多这样的变体,相信我知道终于消失了“code-flind”...... ;-)

有任何解决方案、提示、指针等吗?谢谢:-)

【问题讨论】:

  • 有没有人能解决这个问题?

标签: uiview alassetslibrary custom-cell


【解决方案1】:

已解决!

原来该块没有保留上述“_image”-变量中的图像。只需将“_image”传递给块内的局部变量(如 UIImage“myImage”)以保留它即可解决。现在它工作得很好,我的单元格按预期从资产库中获取图像:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多