Afnetworking is easy to use to download images

Source: Internet
Author: User

After asihttprequest releases a message that is no longer maintained, afnetworking will be a good choice if we do not use CDN.
Bottom site: https://github.com/AFNetworking/AFNetworking

After the download, you can directly access the xcode project and use it. Remember to add systemconfiguration. framework.

Example reference:

Add afnetworkactivityindicatormanager to application: didfinishlaunchingwitexceptions.
Remember

# Import "afnetworkactivityindicatormanager. H"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];    self.window.backgroundColor = [UIColor whiteColor];     [self.window makeKeyAndVisible];    return YES;}

This is a common download network resource (in JSON format)
Remember
# Import "afhttpclient. H"
# Import "afhttprequestoperation. H"
# Import "jsonkit. H"

Nsurl * url = [nsurl urlwithstring: @ "http://www.domain.com"]; afhttpclient * httpclient = [[afhttpclient alloc] initwithbaseurl: url]; nsstring * _ Path = [nsstring stringwithformat: @ "/user_login/% @/", username, password]; nsmutableurlrequest * request = [httpclient requestwithmethod: @ "Post" Path: _ path parameters: Nil]; [httpclient release]; afhttprequestoperation * operation = [[afhttprequestoperation alloc] initwithrequest: request]; [operation duration: ^ (afhttprequestoperation * operation, Id responseobject ){// After the download is successful, use jsonkit to convert the string to nsdictionary or nsarray formatNsdictionary * deserializeddata = [operation. responsestring objectfromjsonstring];} failure: ^ (afhttprequestoperation * operation, nserror * error ){// Handle download failure}]; Nsoperationqueue * queue = [[nsoperationqueue alloc] init] autorelease]; [queue addoperation: Operation];

After completing the previous step, we will get the absolute address of some images, and download the images asynchronously based on these URLs.
Remember # import "uiimageview + afnetworking. H"
The simple method is

[Imageview setimagewithurl: @ "absolute path of image"];

Complex operations: You can trigger some events after the image is downloaded.

Nsurl * url = [nsurl urlwithstring: @ image absolute path "]; nsmutableurlrequest * request = [required requestwithurl: URL cachepolicy: Required timeoutinterval: 30.0]; [imageview failed: Request placeholderimage: nil success: ^ (nsurlrequest * request, nshttpurlresponse * response, uiimage * image) {nslog (@ "image downloaded successfully! Do something ");} failure: ^ (nsurlrequest * request, nshttpurlresponse * response, nserror * error) {nslog (@ image download successful! Do something "") ;}]; ""
From: http://blog.csdn.net/nogodoss/article/details/8243316

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.