2013-01-25 43 views
5

Tôi đã có một gaussian blur mà tôi đang làm cho một ứng dụng.Gaussian Blur Filter Reversal: iOS

//Get a UIImage from the UIView 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
    [self.view.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:2] forKey:@"inputRadius"]; 
    CIImage *resultImage = [gaussianBlurFilter valueForKey:@"outputImage"]; 
    UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage]; 

    //Place the UIImage in a UIImageView 
    newView = [[UIImageView alloc] initWithFrame:self.view.bounds]; 
    newView.image = endImage; 
    [self.view addSubview:newView]; 

Nó hoạt động tốt, nhưng tôi muốn có thể đảo ngược và trả lại chế độ xem trở lại bình thường.

Làm cách nào tôi có thể thực hiện việc này vì chỉ đơn giản là xóa chế độ xem mờ khỏi giám sát của nó không hoạt động. Tôi cũng đã thử thiết lập các thuộc tính khác nhau để không có may mắn.

+0

thể bạn không chỉ cần tạo một bản sao của viewImage trước khi mờ được áp dụng và trở lại khi bạn muốn thay đổi? – Ravi

+0

Làm cách nào để tạo bản sao của nó mà không thay đổi cả hai bản sao sau khi áp dụng hiệu ứng nhòe? – jakenberg

Trả lời

3

Giữ một con trỏ đến viewImage trong một tài sản

@property (nonatomic, strong) UIImage* originalImage; 

rồi sau

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

thêm

self.originalImage = viewImage; 

Để trở lại hình ảnh của bạn:

newView.image = self.originalImage; 

khi bạn áp dụng hiệu ứng làm mờ nó không thay đổi viewImage .. bạn đã tạo một CIImage riêng biệt bị mờ và bạn tạo một UIImage mới từ CIImage bị mờ.

+0

Điều này khiến tôi nhận ra rằng những gì tôi đang làm tổng thể là không chính xác. Tôi đang cố gắng làm cho self.view blur của tôi bởi vì tôi đang đặt một số đối tượng tạm thời trên đầu trang của nó. Tôi không muốn một hình ảnh ban đầu, nhưng thay vào đó quay lại chế độ xem mà tôi đã bắt đầu. Câu trả lời của bạn là đúng, nhưng bạn có nghĩ rằng bạn có thể chỉnh sửa câu trả lời của mình để phản ánh điều đó không? Tôi muốn tôi có thể cung cấp cho bạn hai dấu kiểm cho việc này: P – jakenberg

+0

Tôi đã kết thúc chỉ tạo ra một UIView với một mờ trên nó, sau đó thoát khỏi nó theo cách đó. Cảm ơn bạn! – jakenberg

0

Giống như tôi đã nói trong nhận xét trước, bạn có thể tạo thuộc tính/iVar giữ hình ảnh trước khi áp dụng hiệu ứng và hoàn nguyên trong trường hợp bạn muốn hoàn tác.

if(!_originalImage) 
    _originalImage = [[UIImage alloc] init]; 

_originalImage = viewImage; //(Creates a copy, not a C-Type pass-by-reference) 

// Do your Blur Stuff 

// Now somewhere down the line in your program, if you don't like the blur and the user would like to undo: 

viewImage = _originalImage; 

Theo nhận xét của bạn về câu trả lời của @ HeWas, bạn không nên làm mờ chế độ xem hoàn toàn. Nếu chế độ xem bị mờ, có điều gì đó khác bạn đang làm sai ở nơi khác trong chương trình của mình.

1
  • Điều đầu tiên tôi thấy trong mã của bạn là bạn không áp dụng bộ lọc trong tác vụ không đồng bộ. làm mờ thời gian hình ảnh mất, vì vậy nếu bạn không muốn đóng băng các chủ đề chính bạn nên sử dụng:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    //blur the image in a second thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     //set the blurred image to your imageView in the main thread 
    }); 
}); 

  • Để có thể đảo ngược hình ảnh ban đầu, bạn chỉ cần đặt bản sao mờ trong imageView khác xuất hiện trên bản gốc. Trong trường hợp của tôi, hình ảnh gốc không phải là imageView, mà chính là khung nhìn. Vì vậy, tôi chỉ thêm một imageView trong chế độ xem của tôi để đặt hình ảnh mờ và được đặt thành không khi tôi muốn đảo ngược.

  • Cuối cùng, nếu bạn muốn tránh chớp khi bạn thiết lập các hình ảnh bị mờ, bạn có thể chơi với alpha trong hình ảnh động để làm cho một sự chuyển tiếp mềm:

[self.blurredImageView setAlpha: 0.0]; //make the imageView invisible 
[self.blurredImageView setImage:blurredImage]; 
//and after set the image, make it visible slowly. 
[UIView animateWithDuration:0.5 delay:0.1 
          options:UIViewAnimationOptionCurveEaseInOut 
         animations:^{ 
          [self.blurredImageView setAlpha: 1.0]; 
         } 
         completion:nil]; 

  • Đây là com của tôi phương pháp plete:

- (void)makeBlurredScreenShot{ 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    CIContext *context = [CIContext contextWithOptions:nil]; 
    CIImage *sourceImage = [CIImage imageWithCGImage:imageView.CGImage]; 

    // Apply clamp filter: 
    // this is needed because the CIGaussianBlur when applied makes 
    // a trasparent border around the image 
    NSString *clampFilterName = @"CIAffineClamp"; 
    CIFilter *clamp = [CIFilter filterWithName:clampFilterName]; 
    if (!clamp) 
     return; 

    [clamp setValue:sourceImage forKey:kCIInputImageKey]; 
    CIImage *clampResult = [clamp valueForKey:kCIOutputImageKey]; 

    // Apply Gaussian Blur filter 
    NSString *gaussianBlurFilterName = @"CIGaussianBlur"; 
    CIFilter *gaussianBlur   = [CIFilter filterWithName:gaussianBlurFilterName]; 
    if (!gaussianBlur) 
     return; 

    [gaussianBlur setValue:clampResult forKey:kCIInputImageKey]; 
    [gaussianBlur setValue:[NSNumber numberWithFloat:8.0] forKey:@"inputRadius"]; 

    CIImage *gaussianBlurResult = [gaussianBlur valueForKey:kCIOutputImageKey]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     CGImageRef cgImage = [context createCGImage:gaussianBlurResult fromRect:[sourceImage extent]]; 

     UIImage *blurredImage = [UIImage imageWithCGImage:cgImage]; 
     CGImageRelease(cgImage); 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.blurredImageView setAlpha: 0.0]; 
      [self.blurredImageView setImage:blurredImage]; 
      [UIView animateWithDuration:0.5 delay:0.1 
           options:UIViewAnimationOptionCurveEaseInOut 
          animations:^{ 
           [self.blurredImageView setAlpha: 1.0]; 
          } 
          completion:nil]; 
     }); 
    }); 
} 

- (void)removeBlurredScreenShot{ 
    [UIView animateWithDuration:0.5 delay:0.1 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
          [self.blurredImageView setAlpha: 0.0]; 
          } 
        completion:^(BOOL finished) { 
          [self.blurredImageView setImage:nil]; 
        }]; 
}