Add New Class of type NSobject :-
.H file :-
.H file :-
#import <Foundation/Foundation.h>
@interface downloadImageClass : NSObject{
}
+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage;
@end
.M file :-
#import "downloadImageClass.h"
#import <QuartzCore/QuartzCore.h>
@implementation downloadImageClass
+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage
{
NSURL *url = [NSURL URLWithString:urlString];
dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("com.myapp.processsmagequeue", NULL);
dispatch_async(downloadQueue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(callerQueue, ^{
processImage(imageData);
});
});
dispatch_release(downloadQueue);
}
@end
Save Images to document Directory :-
- (void)save_to_document_directory{
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathArr objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *dirPath1 = [path stringByAppendingPathComponent:@"FolderNserverame"];
self.dirPath = dirPath1;
if (![fileManager fileExistsAtPath:dirPath1]) {
[fileManager createDirectoryAtPath:dirPath1 withIntermediateDirectories:NO attributes:nil error:nil];
}
NSError *error;
for (NSString *file in [fileManager contentsOfDirectoryAtPath:dirPath1 error:&error]) {
BOOL success = [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirPath1, file] error:&error];
if (!success || error) {
// it failed.
}
}
for (int i = 0 ; i < [myimagesArray count]; i++) {
[WebImageOperations processImageDataWithURLString:[myimagesArray objectAtIndex:i] andBlock:^(NSData *imageData) {
NSString *imageName = [[[self.responseDataDic objectAtIndex:i]objectForKey:@"Name"]stringByAppendingString:@".png"];
NSLog(@"imageName....%@",imageName);
if (![fileManager fileExistsAtPath:[dirPath1 stringByAppendingPathComponent:imageName]]) {
[fileManager createFileAtPath:[dirPath1 stringByAppendingPathComponent:imageName] contents:imageData attributes:nil];
}
}];
}
}