IOS reads the URL image and stores it to the local instance

Time:2020-10-20

Examples are as follows:

dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
dispatch_async(globalQueue, ^{
 
 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:banarModel.avatar.url]];
 UIImage *image = [UIImage imag eWithData:data ]; // get pictures
 
 //Local sandbox directory
 NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
 //Get the path named "myimage" in the local sandbox, which is the name of the saved image
 NSString *imageFilePath = [path stringByAppendingPathComponent:@"adGuideImage"];
 //Write the obtained image into the local sandbox, where 0.5 represents the compression ratio, and 1 represents no compression. The smaller the value, the greater the compression ratio
 BOOL success = [UIImageJPEGRepresentation(image, 0.5) writeToFile:imageFilePath atomically:YES];
 if (success){
  Nslog (@ "picture saved successfully");
 }else{
  Nslog (@ "picture save failed");
 }
});

The above example of IOS reading URL image and storing it locally is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developeppaer more.