`

Download an Image and Save it as PNG or JPEG in iPhone SDK

阅读更多
[url]http://blog.objectgraph.com/index.php/2010/04/05/download-an-image-and-save-it-as-png-or-jpeg-in-iphone-sdk/
[/url]
Here is a quick example to save an image as JPEG or PNG. In this tutorial, I’m getting the image from the Internet then I’m saving it as JPEG and PNG.

Key points:

    * NSData to retrieve the image from the URL
    * NSDocumentDirectory to find Document folder’s Path
    * UIImagePNGRepresentation to save it as PNG
    * UIImageJPEGRepresentation to save it as JPEG
	NSLog(@"Downloading...");
	// Get an image from the URL below
	UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.objectgraph.com/images/og_logo.png"]]];
 
	NSLog(@"%f,%f",image.size.width,image.size.height);
 
	// Let's save the file into Document folder.
	// You can also change this to your desktop for testing. (e.g. /Users/kiichi/Desktop/)
	// NSString *deskTopDir = @"/Users/kiichi/Desktop";
 
	NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 
	// If you go to the folder below, you will find those pictures
	NSLog(@"%@",docDir);
 
	NSLog(@"saving png");
	NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
	NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
	[data1 writeToFile:pngFilePath atomically:YES];
 
	NSLog(@"saving jpeg");
	NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
	NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
	[data2 writeToFile:jpegFilePath atomically:YES];
 
	NSLog(@"saving image done");
 
	[image release];
分享到:
评论

相关推荐

    IPhone sdk旧版下载地址

    - 下载地址:http://developer.apple.com/ios/download.action?path=/iphone/iphone_sdk_3.2__final/xcode_3.2.2_and_iphone_sdk_3.2_final.dmg #### 三、注意事项 - 在选择下载哪个版本之前,请确保您的计算机...

    各个版本的Xcode/IOS SDK官网下载

    - [Xcode 3.2.3 and iPhone SDK 4 GM Seed](http://developer.apple.com/ios/download.action?path=/iphone/iphone_sdk_4_gm_seed/xcode_3.2.3_and_iphone_s) 通过以上对各版本Xcode和iOS SDK的详细介绍,我们可以...

    Apress.Beginning.iPhone.3.Development.Exploring.the.iPhone.SDK.Jul.2009.pdf_1

    soup-to-nuts course in iPhone and iPod touch programming. The book starts with the basics, walking you through the process of downloading and installing Apple's free iPhone SDK, and then stepping...

    Apress.Beginning.iPhone.3.Development.Exploring.the.iPhone.SDK.Jul.2009.pdf_2

    soup-to-nuts course in iPhone and iPod touch programming. The book starts with the basics, walking you through the process of downloading and installing Apple's free iPhone SDK, and then stepping...

    iOS 9 SDK Development: Creating iPhone and iPad Apps with Swift

    iOS 9 SDK Development: Creating iPhone and iPad Apps with Swift English | ISBN: 1680501321 | 2016 iOS 9 gives developers new tools for creating apps for iPhone and iPad, and our new edition of the ...

    iPhone SDK Programming, A Beginner's Guide

    Nearly every chapter of iPhone SDK Programming: A Beginner’s Guide consists of a self-contained project, with the corresponding Xcode available for download and modification. The book is designed ...

    Beginning iPhone 4 Development Exploring the iOS SDK

    and you’ll learn techniques to save and retrieve your data using SQLite, iPhone’s built-in database management system and Core Data, the standard for persistence that Apple brought to iOS with the ...

    android sdk 自带 实例(samples)

    You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that ...

    save-html-as-image:将HTML(DOM)下载到图片(JPG,PNG)

    这可以用于很多事情,可以将HTML(DOM)保存为图像(JPG,PNG),并且可以将SVG图像转换为PNG。 将SVG转换为PNG对于Safari浏览器兼容性非常有用,因为它无法呈现...//download the node as png. Image (2019-12-01).p

    Teleport Pro

    If your browser asks you whether to Open it or Save it, choose "Open." (If you have to save it, remember where you saved it, and double-click it to run it after the download is complete.) ...

    Kinect with MS-SDK

    1. Install Kinect SDK or Runtime as explained in Readme-Kinect-MsSdk.pdf. 2. Download the example. 3. Open and run scene SensorKinectMsSDK. Troubleshooting: * If you get DllNotFoundException, make ...

    Learn CSS in One Day and Learn It Well (Volume 2)

    Each CSS chapter comes with an end-of-chapter exercise where you get to practice the different CSS properties covered in the chapter and see first hand how different CSS values affect the design of ...

    [iPhone开发书籍大全].iPhone.Games.Projects(Apress.2009).pdf

    How to optimize games using iPhone SDK tools such as Instruments and Shark, and how to optimize your graphics and sound files for the best performance Insight into the art and craft of game design ...

    iPhone_Games_Projects

    * How to optimize games using iPhone SDK tools such as Instruments and Shark, and how to optimize your graphics and sound files for the best performance * Insight into the art and craft of game ...

    android-sdk-windows

    not contain an Android platform or any third-party libraries. In fact, it doesn't even have all the tools you need to develop an application. In order to start developing applications, you must ...

    Image Download(图片下载)

    it can download the images of website, forum, album, and jump to the next page automatically. and the user also can help the app to find the next button/page, so that it can automatically download ...

    iPhoneOS14.5

    iPhoneOS14.5

    convert html body to image and download

    标题 "convert html body to image and download" 描述的是一个技术过程,即将HTML页面的body部分转换为图像并进行下载。这个过程在Web开发中有时很有用,比如制作网页快照或者优化社交媒体分享预览。主要涉及到的...

    Swift5:Exploring the iOS SDK, 5th Edition-June 2, 2019-5th.pdf

    Assuming little or no working knowledge of the Swift programming language, and written in a friendly, easy-to-follow style, this book offers a comprehensive course in iPhone and iPad programming....

Global site tag (gtag.js) - Google Analytics