下载后iphone Image不会出现

时间:2014-05-27 08:40:38

标签: ios iphone objective-c

我正在构建一个简单的应用程序来显示图像。

我发送图片网址,应该下载。

这是我的代码:

#import "ImageViewController.h"

@interface ImageViewController ()
@property (nonatomic, strong) UIImageView * imageView;
@property (nonatomic, strong) UIImage * image;
@end

@implementation ImageViewController

-(void)setImageURL:(NSURL *)imageURL{
    _imageURL = imageURL;
    self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
}

-(UIImageView*)imageView{
    if(!_imageView)
        return [[UIImageView alloc]init];
    return _imageView;
}


-(UIImage*)image{
    return self.imageView.image;
}

-(void)setImage:(UIImage *)image{
    self.imageView.image = image;
    [self.imageView sizeToFit];
}

-(void)viewDidLoad{
    [self.view addSubview:self.imageView];
    NSLog(@"Image Url = %@", self.imageURL);
}

@end

我称之为:

if ([segue.destinationViewController isKindOfClass:[ImageViewController class]]){
        ImageViewController* ivc = (ImageViewController*)segue.destinationViewController;
        ivc.imageURL = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"http://images.apple.com/v/iphone-5s/gallery/a/images/download/%@.jpg", segue.identifier]];

我100%确定传递的url是正确的,因为我已经通过调试

检查了

图像绝对正在下载,因为它的尺寸很大,而且下载时屏幕会阻塞。

问题是完成下载后imageView没有显示它。

你可以帮帮我吗?

修改

头文件

#import <UIKit/UIKit.h>

@interface ImageViewController : UIViewController
@property(nonatomic, strong) NSURL* imageURL;
@end

这是我的所有代码,因此您可以根据需要进行测试。

EDIT2

我有一个滚动视图,也许这就是问题? 请检查图像

enter image description here

4 个答案:

答案 0 :(得分:1)

enter image description here请尝试编辑您的代码,因为它对我有用,在您的情况下问题可能是因为您从不同的视图控制器传递URL:

[self.imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/v/iphone-5s/gallery/a/images/download/photo_1.jpg"]]]];

您可以从附加的屏幕截图中查看..

答案 1 :(得分:1)

设置imageView的帧而不是发送sizeToFit方法。

-(void)setImage:(UIImage *)image{
    self.imageView.image = image;
    // [self.imageView sizeToFit];
    self.imageView.frame = CGRectMake(100.0, 100.0, image.size.width, image.size.height) ;
}

编辑:

我看到有一个关于&#39; self.view&#39;的滚动查看,因此它可能会阻止self.imageView,如果您希望它是最顶层的,请尝试使用[self.view bringSubviewToFront:self.imageView] ;将其置于最前面self.view上的所有子视图。

答案 2 :(得分:1)

imageView属性的getter中存在问题。您没有初始化属性,但每次都返回不同的对象。替换行:

return [[UIImageView alloc]init]; 

通过

_imageView = [[UIImageView alloc]init];

答案 3 :(得分:0)

您没有将新创建的imageView分配给_imageView。因此,每次触发imageView方法时,它都会返回一个新的UIImageView。试试这个...

- (UIImageView*)imageView
{
    if(!_imageView) {
        _imageView = [[UIImageView alloc]init];
        return _imageView;
    }

    return _imageView;
}
相关问题