【问题标题】:UIImageview is not showing imageUIImageview 不显示图像
【发布时间】:2014-03-08 06:10:17
【问题描述】:

这是我用来显示 PGM 格式图像的代码,它之前工作过,如果我将它用作单独的项目,它也工作但现在不工作,我不知道为什么它不工作。有人请帮帮我。

- (void)viewDidLoad
{
[super viewDidLoad];

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGPoint center = self.view.center;
activityIndicator.frame= CGRectMake(center.x, center.y, 37, 37);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(center.x-30, center.y+40, 160, 30)];
[label setText:@"Calculating..."];
[self.view addSubview:activityIndicator];
[self.view addSubview:label];
[activityIndicator startAnimating];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/////////////// read file ///////////
    FILE *file = fopen("/Users/Sumit/Desktop/test/out.pgm", "rb");
    if (file == NULL) {
        NSLog(@"File Not Found");
        return;
    }
    char name[20], secondline[10];
    int w=0, h=0, colors=0;
    fscanf(file, "%s", name);
    fscanf(file, "%s", secondline);
    fscanf(file, "%d %d %d", &w, &h, &colors);
    //printf("%d %d\n", w, h);
    unsigned char * imagedata = (unsigned char *) malloc(sizeof(unsigned char)*w*h);
    fseek(file, 1, SEEK_CUR);
    int i=0;
    while (!feof(file)) {
        int val = getc(file);
        imagedata[i] = val;
        i++;
    }
    fclose(file);
    ////////////////// end read file /////////////

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
    CFDataRef rgbData = CFDataCreate(NULL, imagedata, w * h);
    CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);
    CGImageRef rgbImageRef = CGImageCreate(w, h, 8, 8, w , colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault);
    CFRelease(rgbData);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorspace);
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
    UIImage * newimage = [UIImage imageWithCGImage:rgbImageRef];
    //imageview.frame = CGRectMake(imageview.frame.origin.x,imageview.frame.origin.y, w, h);
    [imageview setImage:newimage];
    [activityIndicator removeFromSuperview];
    [label removeFromSuperview];

    CGSize boundsSize = scrollview.bounds.size;
    CGRect frameToCenter = imageview.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    imageview.frame = frameToCenter;
    [scrollview addSubview:imageview];
    CGImageRelease(rgbImageRef);
    dispatch_async(dispatch_get_main_queue(), ^(void){
        //[alertView dismissWithClickedButtonIndex:0 animated:YES];
    });
});

// Do any additional setup after loading the view.
}

【问题讨论】:

    标签: ios objective-c ipad uiimageview


    【解决方案1】:

    您必须在主线程上进行每个 UI 修改。您将图像 view.image 设置在另一个线程上。这是你的问题。

    dispatch_async(dispatch_get_main_queue(), ^{
        imageview.image = newimage;
    });
    

    您的实际代码会影响,您的图像不会在 [imageview setImage:newimage]; 之后立即设置。行,因为它在后台线程上。稍后会设置。

    【讨论】:

    • 没错!作为最佳实践,所有 UI 模块都应该在主线程上完成!
    • 我明白了你的意思,但它仍然无法正常工作,几天前它还在工作,我不知道它是如何突然停止工作的
    • 检查您的新图像是否不为零,例如,将图像 view.backgroundcolor 设置为橙色,以确保您的图像视图位于正确的位置,具有正确的宽度和高度。
    • 您确定您的图像视图宽度和高度不为 0 吗?
    • 是的,高度和宽度不是 0,即使我尝试将背景色设为红色,但它没有显示任何内容
    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    相关资源
    最近更新 更多