Skip to content

Instantly share code, notes, and snippets.

View quentinfasquel's full-sized avatar

Quentin quentinfasquel

View GitHub Profile
guard let filter = CIFitler(name: "CIBlendWithMask") else {
// Could not find the built-in filter
return nil
}
let backgroundImage = CIImage(color: .clear).cropped(to: inputImage.extent)
filter.setValue(backgroundImage, forKey: kCIInputBackgroundImageKey)
filter.setValue(inputImage, forKey: kCIInputImageKey)
filter.setValue(maskImage, forKey: kCIInputMaskImageKey)
return filter.outputImage
let myKernel = try CIKernel(functionName: "doNothing")
import CoreImage
import Metal
private func defaultMetalLibrary() throws -> Data {
let url = Bundle.main.url(forResource: "default", withExtension: "metallib")!
return try Data(contentsOf: url)
}
extension CIKernel {
/// Init CI kernel with just a `functionName` directly from default metal library
#include <metal_stdlib>
using namespace metal;
#include <CoreImage/CoreImage.h>
extern "C" {
namespace coreimage {
float4 doNothing(sampler src) {
return src.sample(src.coord());
}
}
import CoreImage
class AlphaFrameFilter: CIFilter {
static var kernel: CIColorKernel? = {
return CIColorKernel(source: """
kernel vec4 alphaFrame(__sample s, __sample m) {
return vec4( s.rgb, m.r );
}
""")
}()
#import "LeakTest.h"
#import "FailingObject.h"
@interface LeakTest ()
@property (nonatomic, strong) FailingObject *object;
@end
@implementation LeakTest
- (void)performFailure {
// CatchingException.swift
{
let failingObject = MyFailingObject()
if let exception = objc_tryCatch({ failingObject.throwException() }) {
// Here we caught the exception, but we actually leaked failingObject
}
// Let's check that it's actually leaking
// MyFailingObject.m
@implementation MyFailingObject
- (void)throwException {
@throw [NSException exceptionWithName:NSGenericException
reason:@"An exception on purpose"
userInfo:nil];
}
// Given a block `let mayRaiseAnException: () -> Void`
if let exception = objc_tryCatch(mayRaiseAnException) {
// Here is your caugth exception
}
#import "objc_tryCatch.h"
NSException * _Nullable objc_tryCatch(void (^ _Nonnull block)(void)) {
@try {
block();
return nil;
} @catch (NSException *exception) {
return exception;
}
}