[Code Note] various methods for displaying images, and displaying images with code notes

Source: Internet
Author: User

[Code Note] various methods for displaying images, and displaying images with code notes

Code:

-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. UIImageView * img = [[UIImageView alloc] initWithFrame: CGRectMake (50,100,200,200)]; img. backgroundColor = [UIColor redColor]; [self. view addSubview: img]; // download, save, and use UIImageView to display NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; NSLog (@ "Save path: % @", documentsDirectoryPath); // Get Image From URL UIImage * imageFromURL = [self getImageFromURL: @ "http://file.duteba.com/phone/2009/04/5/ftGxL8kcUL.jpg"]; // Save Image to Directory [self saveImage: imageFromURL withFileName: @ "MyImage" ofType: @ "jpg" inDirectory: documentsDirectoryPath]; // Load Image From Directory UIImage * imageFromWeb = [self loadImage: @ "MyImage" ofType: @ "jpg" inDirectory: documentsDirectoryPath]; [img setImage: imageFromWeb]; // obtain all file names in the directory. NSArray * file = [[[NSFileManager alloc] init] subpathsAtPath: documentsDirectoryPath]; NSLog (@ "% lu", (unsigned long) [file count]); NSLog (@ "% @", file);} // download an image from the network-(UIImage *) getImageFromURL :( NSString *) fileURL {NSLog (@ "execute image download function"); UIImage * result; NSData * data = [NSData dataWithContentsOfURL: [NSURL URLWithString: fileURL]; result = [UIImage imageWithData: data]; return result;} // Save the downloaded image locally-(void) saveImage :( UIImage *) image withFileName :( NSString *) imageName ofType :( NSString *) extension inDirectory :( NSString *) directoryPath {if ([[extension lowercaseString] isEqualToString: @ "png"]) {[UIImagePNGRepresentation (image) writeToFile: [directoryPath scheme: [NSString stringWithFormat: @ "% @. % @ ", imageName, @" png "] options: NSAtomicWrite error: nil];} else if ([[extension lowercaseString] isEqualToString: @ "jpg"] | [[extension lowercaseString] isEqualToString: @ "jpeg"]) {[UIImageJPEGRepresentation (image, 1.0) writeToFile: [directoryPath stringByAppendingPathComponent: [NSString stringWithFormat: @ "% @. % @ ", imageName, @" jpg "] options: NSAtomicWrite error: nil];} else {// ALog (@" Image Save Failed \ nExtension: (% @) is not recognized, use (PNG/JPG) ", extension); NSLog (@" ") ;}// read locally saved images-(UIImage *) loadImage :( NSString *) fileName ofType :( NSString *) extension inDirectory :( NSString *) directoryPath {UIImage * result = [UIImage imageWithContentsOfFile: [NSString stringWithFormat: @ "% @/% @. % @ ", directoryPath, fileName, extension]; return result;}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .}

 

Output:

13:43:09. 989 download, save, and use UIImageView to display [7260: 182365] Save path from Save: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/connectors/Documents2015-10-13 13:43:09. 989 download an image from the network, save it, and use UIImageView to display [7260: 182365]. Execute the image download function 13:43:10. 231 download the image from the network, save it, and use UIImageView to display [7260: 182365] 02015-10-13 13:43:10. 231 download, save, and use UIImageView to display [7260: 182365] ()

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.