我有一个名为IBOutlet
的{{1}}到UIImageView
。默认情况下,它通过故事板添加占位符图像。
如果图像已存在,则使用attachmentImage
显示。这适用于显示图像。来自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
如此接近。
答案 0 :(得分:1)
我会先尝试删除setNeedsDisplay - 在大多数情况下UIImageView不需要
如果这不起作用,似乎代码的第一部分在第二部分之前由于某种原因而运行。我会进行一些日志记录调用来检查执行的真正顺序。
如果所有其他方法都失败了,你可以通过延迟通话实现一个真正的hacky解决方案..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{ .... }
如果这解决了它,它可以给你一些在这2秒内运行的意外代码的线索..
答案 1 :(得分: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;
而恢复。
答案 2 :(得分:0)
我认为委托方法
- (void)takeController:(FDTakeController *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info
你说的是在imageview网址中设置图片之前调用。 因此,您可以尝试了解何时调用此方法,或者暂时可以明确地调用委托方法:
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 ];
}
});
答案 3 :(得分: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...
}
答案 4 :(得分: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];
}
答案 5 :(得分:0)
对我来说,问题是从URL下载图片需要时间。 在这种情况下,我会使用SDWebImage或已经提到的PromiseKit。
SDWebImage也允许缓存图片。