Skip to content

Instantly share code, notes, and snippets.

@camdenfullmer
Last active February 18, 2024 03:05
Show Gist options
  • Select an option

  • Save camdenfullmer/b287f634acd089f4b92f to your computer and use it in GitHub Desktop.

Select an option

Save camdenfullmer/b287f634acd089f4b92f to your computer and use it in GitHub Desktop.

Revisions

  1. camdenfullmer revised this gist Feb 25, 2016. No changes.
  2. camdenfullmer created this gist Feb 25, 2016.
    39 changes: 39 additions & 0 deletions CMFWallpaper.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    @implementation CMFWallpaper

    + (void)setImage:(UIImage *)image {
    NSAssert([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized, @"access to photos is needed to set the wallpaper");

    NSString *path;
    #if TARGET_OS_SIMULATOR
    path = @"/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibrary.framework";
    #else
    path = @"/System/Library/PrivateFrameworks/PhotoLibrary.framework";
    #endif
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    [bundle load];

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wundeclared-selector"

    // Instantiate the controller.
    id class = NSClassFromString(@"PLStaticWallpaperImageViewController");
    id controller = [[class alloc] performSelector:@selector(initWithUIImage:) withObject:image];

    // Select what wallpaper mode.
    // 0 - Both lock screen and home screen.
    // 1 - Home screen only.
    // 2 - Lock screen only.
    int wallpaperMode = 0;
    [controller setValue:@(wallpaperMode) forKey:@"wallpaperMode"];

    // Tell the controller to save the data.
    [controller setValue:@YES forKey:@"saveWallpaperData"];

    // Save the photo.
    [controller performSelector:@selector(_savePhoto) withObject:nil];

    #pragma clang diagnostic pop

    }

    @end