ios - UIImageview 不显示图像

标签 ios objective-c ipad uiimageview

这是我用来显示 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.
}

最佳答案

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

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

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

关于ios - UIImageview 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22265812/

相关文章:

ios - 如何在 IOS Swift 中检测飞行模式/飞行模式

ios - 如何在 Objective-C 的 AlertView 中通过 http url 显示图像?

ios - 如果 UIInterfaceOrientation 横向值相同,我应该如何确定横向 View 的方向?

iphone - 如何让 iOS 应用程序在 iPad 上仅使用 960 x 640 分辨率?

iphone - iPad上的UIWebView初始化崩溃

iphone - 通过应用内购买从付费应用到免费应用

ios - 使用 CAShapeLayer 填充颜色问题

ios - NSPersistentStoreCoordinator在ios中没有持久化存储(Schema不匹配或迁移失败)核心数据iOS

javascript - 使用 Objective-C 从 HTML 中获取由 javascript 生成的表

objective-c - 这可以缩短吗?字体=字体?字体 : defaultFont;