在ios中设置使用块查看的背景图像?
当用户点击按钮时,我将导航到另一个视图,并设置该视图的背景图像。但是图像来自URL。要转换为NSData格式需要一些时间。因此,如何使用块来实现这一点 我使用了下面的代码,但没有运气在ios中设置使用块查看的背景图像?,ios,objective-c-blocks,nsdata,Ios,Objective C Blocks,Nsdata,当用户点击按钮时,我将导航到另一个视图,并设置该视图的背景图像。但是图像来自URL。要转换为NSData格式需要一些时间。因此,如何使用块来实现这一点 我使用了下面的代码,但没有运气 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self setBackGroundImageView]; }); - (void)setBackGroundImageView {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self setBackGroundImageView];
});
- (void)setBackGroundImageView {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:sharedDelegate.fbAppScopeId]];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
if (imageData) {
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[self squareImageWithImage:[UIImage imageWithData:imageData] scaledToSize:CGSizeMake(100, 100)]];
CGFloat ratio = backgroundView.frame.size.width/screenWidth;
CGRect imageFrame = backgroundView.frame;
imageFrame.size.width = screenWidth;
imageFrame.size.height = backgroundView.frame.size.height/ratio;
if (imageFrame.size.height<screenHeight) {
ratio = backgroundView.frame.size.height/screenHeight;
imageFrame.size.height = screenHeight;
imageFrame.size.width = backgroundView.frame.size.width/ratio;
}
backgroundView.frame = imageFrame;
backgroundView.center = CGPointMake(screenWidth/2, backgroundView.center.y);
backgroundView.alpha = 0.5;
[self.view addSubview:backgroundView];
}
}
}
dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u DEFAULT,0)^{
[自我挫折视野];
});
-(无效)setBackGroundImageView{
NSData*imageData=[NSData DATA WITHCONTENTSOFURL:[NSURL URLWithString:sharedDelegate.fbAppScopeId]];
调度同步(调度获取主队列(),^(无效){
if(图像数据){
UIImageView*backgroundView=[[UIImageView alloc]initWithImage:[UIImageImageWithImage:[UIImageImageWithData:imageData]scaledToSize:cSizeMake(100100)];
CGFloat比率=backgroundView.frame.size.width/screenWidth;
CGRect imageFrame=backgroundView.frame;
imageFrame.size.width=屏幕宽度;
imageFrame.size.height=backgroundView.frame.size.height/比率;
如果(imageFrame.size.height不能在主线程以外的其他线程上执行UI事件(添加或删除视图)。如果执行此操作,则可能会出现错误行为
若您仍要这样做,请在主线程上添加imageView并在块中设置其图像
另外,您可以使用block预加载(在应用程序启动时)图像数据,一旦加载完成,您可以将其用作imageView.image=preload image;
您的问题在于这一行代码:
dispatch_sync(dispatch_get_main_queue()
正确使用dispatch\u async是
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
});
});
请将setBackGroundImageView
上的dispatch\u sync
更改为dispatch\u async
此外,我还建议您将所有异步代码放在一个方法中,正如我前面提到的。类似于以下内容:
- (void)setBackGroundImageView {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:sharedDelegate.fbAppScopeId]];
// YOUR PROBLEM WAS HERE
dispatch_async(dispatch_get_main_queue(), ^(void) {
if (imageData) {
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[self squareImageWithImage:[UIImage imageWithData:imageData] scaledToSize:CGSizeMake(100, 100)]];
CGFloat ratio = backgroundView.frame.size.width/screenWidth;
CGRect imageFrame = backgroundView.frame;
imageFrame.size.width = screenWidth;
imageFrame.size.height = backgroundView.frame.size.height/ratio;
if (imageFrame.size.height<screenHeight) {
ratio = backgroundView.frame.size.height/screenHeight;
imageFrame.size.height = screenHeight;
imageFrame.size.width = backgroundView.frame.size.width/ratio;
}
backgroundView.frame = imageFrame;
backgroundView.center = CGPointMake(screenWidth/2, backgroundView.center.y);
backgroundView.alpha = 0.5;
[self.view addSubview:backgroundView];
}
}
});
}
-(无效)setBackGroundImageView{
调度异步(调度获取全局队列(调度队列优先级默认为0)^{
NSData*imageData=[NSData DATA WITHCONTENTSOFURL:[NSURL URLWithString:sharedDelegate.fbAppScopeId]];
//你的问题就在这里
dispatch\u async(dispatch\u get\u main\u queue(),^(void){
if(图像数据){
UIImageView*backgroundView=[[UIImageView alloc]initWithImage:[UIImageImageWithImage:[UIImageImageWithData:imageData]scaledToSize:cSizeMake(100100)];
CGFloat比率=backgroundView.frame.size.width/screenWidth;
CGRect imageFrame=backgroundView.frame;
imageFrame.size.width=屏幕宽度;
imageFrame.size.height=backgroundView.frame.size.height/比率;
如果(imageFrame.size.heightyes)可以,它将无法按预期工作-但他实际上正在主线程上执行dispatch\u get\u main\u queue()
您提供的代码对我有效,您是否尝试过使用调试器来确保backgroundView图像有效?