2014-09-01 89 views
0

我有一个IBOutletUIImageView调用attachmentImage。默认情况下,它具有通过故事板添加的占位符图像。不能更改UIImageView图像

如果图像已经存在,则使用setImageWithURL显示。这工作并显示图像。调用从viewDidLoad

[self.attachmentImage setImageWithURL:[NSURL URLWithString:attachmentImageURL]]; 
self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill; 
self.attachmentImage.clipsToBounds = YES; 
[attachmentImage setNeedsDisplay]; 

如果图像不存在,它从库中选择并添加像这样;

- (void)takeController:(FDTakeController *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info { 
    [attachmentImage setImage:nil]; 
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 
    self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill; 
    [self.attachmentImage setImage:photo]; 
    self.attachmentImage.clipsToBounds = YES; 
    [attachmentImage setNeedsDisplay]; 
} 

如果没有添加图像(除了占位符),上传就会起作用。但是,如果图像视图的图像设置为setImageWithURL,则图像未被替换。

这是怎么发生的?帮帮我!

编辑现在图像确实改变了,但在一两秒后恢复原状。这是我对setImageWithURL函数所做的更改;

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
     dispatch_async(queue, ^{ 
      attachmentImageURL = [NSString stringWithFormat:@"%@%@", BaseURL, imgpath]; 
      NSURL *url = [NSURL URLWithString:attachmentImageURL]; 
      NSData *data = [NSData dataWithContentsOfURL:url]; 

      if (![data isEqualToData:nil]) { 
       UIImage *image = [UIImage imageWithData:data]; 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        attachmentImage.image = image; 
        self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill; 
        self.attachmentImage.clipsToBounds = YES; 
        [attachmentImage setNeedsDisplay]; 

        [self applyImageViewConstraints]; 
       }); 
      } 

     }); 

太近了。

+0

你能更好地解释你想要拥有什么,以及你有什么实际?你也可以张贴一些截图吗? – Serluca 2014-09-03 23:10:59

+0

你正在从服务器获取图像? – 2014-09-08 07:37:35

+0

我想你可能在设置新图片后调用了viewDidload。分开设置默认图像的代码(在viewDidload中),然后添加一个NSLog()以知道它何时被调用。 – 2014-09-09 14:46:03

回答

0

这工作,但无法弄清楚什么是错误的。事做队列:/

viewDidLoad

attachmentImageURL = [NSString stringWithFormat:@"%@%@", BaseURL, imgpath]; 
     NSLog(@"the image url is %@", attachmentImageURL); 
     [self.attachmentImage setImageWithURL:[NSURL URLWithString:attachmentImageURL]]; 
     self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill; 
     self.attachmentImage.clipsToBounds = YES; 
     [attachmentImage setNeedsDisplay]; 
     [self applyImageViewConstraints]; 

而且上传方法。

- (void)takeController:(FDTakeController *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info { 

    NSLog(@"In takeController"); 

    dispatch_async(dispatch_get_main_queue(), ^{attachmentImage.image = nil;}); 

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 
    self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill; 
    dispatch_async(dispatch_get_main_queue(), ^{attachmentImage.image = photo;}); 
    self.attachmentImage.clipsToBounds = YES; 
    selectedImage = photo; 

    [self applyImageViewConstraints]; 
} 
0

我认为你可以清理很多你的代码。

我不知道,如果我知道你想什么做的,但试图实现这种代码:

-(void)viewDidLoad{ 
    [super viewDidLoad]; 
    self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill; // You can also select the content mode in the storyboard 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(queue, ^{ 
     attachmentImageURL = [NSString stringWithFormat:@"%@%@", BaseURL, imgpath]; 
     NSURL *url = [NSURL URLWithString:attachmentImageURL]; 
     NSData *data = [NSData dataWithContentsOfURL:url]; 

     if (data) { 
      UIImage *image = [UIImage imageWithData:data]; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       self.attachmentImage.image = image; 
      }); 
     }else{ 
      // Do something 
     } 

    }); 
} 

也许图像恢复,因为你调用的方法- (void)takeController:(FDTakeController *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info;为时尚早。

0

我想委托方法

- (void)takeController:(FDTakeController *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info 

您是如何评价前图像中的ImageView URL设置被调用。 所以,你可以尝试让知道什么时候该方法被调用或暂时可以调用委托方法明确地想:

dispatch_async(queue, ^{ 
     attachmentImageURL = [NSString stringWithFormat:@"%@%@", BaseURL, imgpath]; 
     NSURL *url = [NSURL URLWithString:attachmentImageURL]; 
     NSData *data = [NSData dataWithContentsOfURL:url]; 

     if (data) { 
      UIImage *image = [UIImage imageWithData:data]; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       self.attachmentImage.image = image; 
      }); 
     }else{ 
      // Do call delegate method with needed arguments supplied 

[fdTakeController.delegate takeController:controller gotPhoto:photo withInfo:info ]; 

     } 

    }); 
1

我会尝试先删除setNeedsDisplay - 在大多数情况下不需要的UIImageView

如果这不起作用,看起来代码的第一部分由于某种原因在第二部分之前运行。我会把一些日志记录调用来检查什么是执行的真正的顺序。

如果一切都失败了,你可以通过延迟调用实现一个真正哈克的解决方案..

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),  
dispatch_get_main_queue(), ^{ .... } 

还是否能解决它,它可以给你那些2秒内部运行的一些意想不到的代码线索..

+0

尝试没有setNeedsDisplay。问题仍在。 – 2014-09-10 19:47:14

0

似乎是一个怪异的行为,也许调用setImageWithURL延迟了设置图像(如果确实存在的话)的代码的执行,也许你可以尝试PromiseKit和改变你的setImageWithURL到这样的事情:

// In your class definition 
PMKPromise* setImageFromURLPromise; 

// In your implementation 
-(void) setImageWithURL { 
    _setImageFromURLPromise = [NSURLConnection GET:@[NSString stringWithFormat:@"%@%@", BaseURL, imgpath]].then(^(UIImage *image) { 
     self.attachmentImage.image = image; 
    }).catch(^(NSError *error){ 
     // error handling if needed? 
    }); 
} 

而且之前实际上是试图从相机载入/照片库中的照片,你可以检查:

if (setImageFromURLPromise == nil || [setImageFromURLPromise resolved]) { 
    // The promise is resolved, no more race conditions or rare behavior, take the photo or load from the library. 
} else { 
    // Display a message that the image from URL is still loading... 
} 
0

在我看来,问题是,它需要时间从URL下载图像。 在这种情况下,我会使用SDWebImage或已经提到PromiseKit

SDWebImage也允许缓存图像。

相关问题