0

I am making an app of live photo/wallpaper. I am downloading image and .mov file from server. Now I am stuck to saving these files in document directory. How can I store the live wallpaper in document directory. Is anyone have idea about this please tell me. Here is my code

SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:[NSURL URLWithString:imgFile]
                         options:0
                        progress:nil                           completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                           if (image && finished) {
        NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentPath = [searchPaths objectAtIndex:0];
        NSString* photoPath =  [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.JPG",strImg]];
        [data writeToFile:photoPath atomically:YES];


                           }
                       }];
NSURL *URL = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * downloadProgress){
    NSLog(@"%f",downloadProgress.fractionCompleted);
    NSString * str = [NSString stringWithFormat:@"%.2f",downloadProgress.fractionCompleted];
    NSDictionary * infoDict = @{@"Download": str,
                                @"id":uniqId};
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"DownloadNotification"
     object:self userInfo:infoDict];



}destination:^ NSURL *(NSURL *targetPath, NSURLResponse *response) {

    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [searchPaths objectAtIndex:0];
    NSString* videoPath =  [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.MOV",strImg]];
    NSURL *videoUrl =[NSURL URLWithString:videoPath];

    NSLog(@"%@",[videoUrl URLByAppendingPathComponent:videoPath]);
    return videoUrl;

} completionHandler:^(NSURLResponse* response, NSURL *filePath, NSError *error) {

    if(error)
    {
        NSLog(@"File Not Dowloaded %@",error);
        [self downloadFile];
    }
    else {
        NSLog(@"%@",filePath);


        [self.downloadRecord removeObjectAtIndex:0];
        if (self.downloadRecord.count > 0) {
            [self downloadFile];
        }
    }
}];
Muhammad Salman
  • 543
  • 4
  • 22

1 Answers1

0

There's no official way to set the wallpaper programatically (or add an image/video to the list of wallpapers) from the inside an app. The closest you can achieve would be saving it to the camera role and then letting the user setting it as a wallpaper from there.

However, I believe Apple have used a private API to achieve this in some of their advertisements, however this isn't documented and using it would cause your app to be rejected from the app store approval.

Greg
  • 890
  • 5
  • 9
  • I know that ... I tried to save pair of live wallpaper using image (.jpg) with video (.MOV) video together (downloaded and saved in document directory), image saved successfully but video fails to save. That's the main point. – Muhammad Salman Jun 27 '16 at 09:56
  • The way you worded it sounded like you were stuck being able to **only** saving it to the documents directory. I'll update my answer. – Greg Jun 27 '16 at 10:12
  • Yes, If you have any solution please tell me. – Muhammad Salman Jun 27 '16 at 12:02