Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ios中设置使用块查看的背景图像?_Ios_Objective C Blocks_Nsdata - Fatal编程技术网

在ios中设置使用块查看的背景图像?

在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 {

当用户点击按钮时,我将导航到另一个视图,并设置该视图的背景图像。但是图像来自URL。要转换为NSData格式需要一些时间。因此,如何使用块来实现这一点

我使用了下面的代码,但没有运气

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图像有效?