“思考/加载”屏幕的模糊图像

时间:2014-11-30 02:56:47

标签: ios xcode

我使用2页来创建带有微调器的模糊图像,我想用它来加载/思考叠加层: http://www.sitepoint.com/all-purpose-loading-view-for-ios/ http://x-code-tutorials.com/2013/06/18/ios7-style-blurred-overlay-in-xcode/

工作正常,但需要进行修改。我有3个问题。

第一个问题: 单击按钮后,似乎需要很长时间才能实现。有什么建议吗?

第二个问题是: 模糊的图像在拍摄时或在视图中设置时向左和向下移动。有什么想法吗? 似乎数字越高,图像的移位越多。

[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"]; 

第3个问题: 我试图在API后端正在执行数据库工作时显示它。如果我不调用RemoveBlurredOverlay然后它显示并工作,但是如果我在所有数据库工作之后调用它将根本不显示。有什么想法吗?需要线程吗?

BlurredOverlay.m

@implementation BlurredOverlay

+(BlurredOverlay *)loadBlurredOverlay:(UIView *)superView {

    BlurredOverlay *blurredOverlay = [[BlurredOverlay alloc] initWithFrame:superView.bounds];

    // Create a new image view, from the image made by our gradient method
    UIImageView *blurredBackground = [[UIImageView alloc] initWithImage:[self   captureBlur:superView]];

    [blurredOverlay addSubview:blurredBackground];

    // This is the new stuff here ;)
    UIActivityIndicatorView *indicator =
    [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];

    //set color
    [indicator setColor:UIColorFromRGB(0x72CE97)];

    // Set the resizing mask so it's not stretched
    indicator.autoresizingMask =
    UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin;

    // Place it in the middle of the view
    indicator.center = CGPointMake(superView.bounds.origin.x + (superView.bounds.size.width / 2), superView.bounds.origin.y + (superView.bounds.size.height / 2));

    // Add it into the spinnerView
    [blurredOverlay addSubview:indicator];

    // Start it spinning! Don't miss this step
    [indicator startAnimating];

    //blurredOverlay.backgroundColor = [UIColor blackColor];
    [superView addSubview:blurredOverlay];

    return blurredOverlay;

}


+ (UIImage *) captureBlur:(UIView *)superView {
    //Get a UIImage from the UIView
    UIGraphicsBeginImageContext(superView.frame.size);
    [superView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //Blur the UIImage
    CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
    [gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
    [gaussianBlurFilter setValue:[NSNumber numberWithFloat: 1] forKey: @"inputRadius"];        //change number to increase/decrease blur
    CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];

    //create UIImage from filtered image
    UIImage *blurrredImage = [[UIImage alloc] initWithCIImage:resultImage];

    return blurrredImage;
}

-(void)removeBlurredOverlay{
    // Take me the hells out of the superView!
    [super removeFromSuperview];
}

@end

MainViewController.m

...
- (IBAction)loginButton:(id)sender {
//Add a blur view to tell uses the app is "thinking"
BlurredOverlay *blurredOverlay = [BlurredOverlay loadBlurredOverlay:self.view];

NSInteger success = 0;


//Check to see if the username or password textfields are empty or email field is in wrong format
if([self validFields]){

    //Try to login user
    success = [self loginUser]; //loginUser sends the http to the back end API that does the database stuff
}
//If successful, go to the View 
if (success) {

    //Remove blurredOverlay
    //[blurredOverlay removeBlurredOverlay]; //This makes it not display at all

    //Seque to the main View
    [self performSegueWithIdentifier:@"loginSuccessSegue" sender:self];
}
else
{
    //Remove blurredOverlay
    //[blurredOverlay removeBlurredOverlay]; //This makes it not display at all

    self.passwordTextField.text = @"";
}

}

1 个答案:

答案 0 :(得分:0)

以下是我对问题1的回答: 线程新,所以建议将不胜感激。 我在BlurredImageView.m中添加了另一种方法来构建视图,并采用模糊的图像。 我将captureBlurredImage方法设为public,并在Login.m中的ViewDidLoad中的一个线程中调用它,然后将模糊图像传递给新的loadBlurredOverlay。我还将登录处理添加到一个线程中。现在真的很快,但是:

问题#3仍然存在!!!! 如果我调用[blurredOverlay removeBlurredOverlay];在LoginViewController.m中调用[self removeFromSuperview];在FuzzyOverlay.m中,BlurredImage和spinner永远不会出现。如果我发表评论,我的工作就像一个魅力,但在登录处理完成后无法解除它。

评论和帮助将不胜感激。如果我们能够深入了解这个问题,我会编辑这个答案。

BlurredImage.m

#import "BlurredOverlay.h"

@implementation BlurredOverlay

+(BlurredOverlay *)loadBlurredOverlay:(UIView *)superView :(UIImage *) blurredImage {

    NSLog(@"In loadBlurredOverlay with parameter blurredImage: %@", blurredImage);

    BlurredOverlay *blurredOverlay = [[BlurredOverlay alloc] initWithFrame:superView.bounds];

    // Create a new image view, from the image made by our gradient method
    UIImageView *blurredBackground = [[UIImageView alloc] initWithImage:blurredImage];

    [blurredOverlay addSubview:blurredBackground];

    // This is the new stuff here ;)
    UIActivityIndicatorView *indicator =
    [[UIActivityIndicatorView alloc]
 initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];

    //set color
    [indicator setColor:UIColorFromRGB(0x72CE97)];

    // Set the resizing mask so it's not stretched
    indicator.autoresizingMask =
    UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin;

    // Place it in the middle of the view
    indicator.center = CGPointMake(superView.bounds.origin.x + (superView.bounds.size.width / 2), superView.bounds.origin.y + (superView.bounds.size.height / 2));

    // Add it into the spinnerView
    [blurredOverlay addSubview:indicator];

    // Start it spinning! Don't miss this step
    [indicator startAnimating];

    //blurredOverlay.backgroundColor = [UIColor blackColor];
    [superView addSubview:blurredOverlay];
    [superView bringSubviewToFront:blurredOverlay];

    return blurredOverlay;

}


+ (UIImage *) captureBlurredImage:(UIView *)superView {
    //Get a UIImage from the UIView
    UIGraphicsBeginImageContext(superView.frame.size);
    [superView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //Blur the UIImage
    CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
    [gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
    [gaussianBlurFilter setValue:[NSNumber numberWithFloat: 1] forKey: @"inputRadius"]; //change number to increase/decrease blur
    CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];

    //create UIImage from filtered image
    UIImage *blurrredImage = [[UIImage alloc] initWithCIImage:resultImage];

    return blurrredImage;
}

-(void)removeBlurredOverlay{
    // Take me the hells out of the superView!
    [self removeFromSuperview];

}

@end

LoginViewController.m

...
-(void)viewDidAppear:(BOOL)animated
{
    //Get a blurred image of the view in a thread<#^(void)block#>
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        self.blurredImage = [BlurredOverlay captureBlurredImage:self.view];

    });
}
...
//Send the username and password to backend for verification
//If verified, go to ViewController
- (IBAction)loginButton:(id)sender {
    __block BOOL success = false;

    //Add a blur view with spinner to tell user the app is processing login information
    BlurredOverlay *blurredOverlay = [BlurredOverlay loadBlurredOverlay:self.view :self.blurredImage];

    //Login user in a thread
    //Get a blurred image of the view in a thread
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        //Check to see if the username or passord texfields are empty or email field is in wrong format
        if([self validFields]){

            //Try to login user
            success = [self loginUser];
        }
        else {
            success = false;
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            //If successful, go to the ViewController
            if (success) {

                //Remove blurredOverlay
                //[blurredOverlay removeBlurredOverlay];

                //Seque to the main ViewController
                [self performSegueWithIdentifier:@"loginSuccessSegue" sender:self];
            }
            else
            {
                //Remove blurredOverlay
                //[blurredOverlay removeBlurredOverlay];

                //Reset passwordTextField
                self.passwordTextField.text = @"";
            }
        });
    });
}
...