Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added two methods for UIImageView+AFNetworking #880

Closed
wants to merge 2 commits into from

3 participants

Luis Ascorbe Mattt Thompson Blake Watters
Luis Ascorbe
  • (void)setImageWithURLOrString:(id)object;
  • (void)setImageWithURLOrString:(id)object placeholderImage:(UIImage *)placeholderImage;
Lascorbe added some commits
Luis Ascorbe Lascorbe Adding two methods
- (void)setImageWithURLOrString:(id)object;
- (void)setImageWithURLOrString:(id)object
                placeholderImage:(UIImage *)placeholderImage;
a909b0e
Luis Ascorbe Lascorbe Added two methods
- (void)setImageWithURLOrString:(id)object;
- (void)setImageWithURLOrString:(id)object
                placeholderImage:(UIImage *)placeholderImage;
7ab0fe6
Mattt Thompson
Owner

Thanks for the pull request, but I don't really see the value in these methods. Increasing the surface area in order to the API just to save a call to NSURL +URLWithString: seems redundant.

Mattt Thompson mattt closed this
Blake Watters

If I were going to go down this road I'd probably just change the signature of setImageWithURL: to accept an id and do type checks on the value passed rather than adding new methods. Plus a good rule of thumb for API design is never compress something down to one line that can already be done in…. one line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2013
  1. Luis Ascorbe

    Adding two methods

    Lascorbe authored
    - (void)setImageWithURLOrString:(id)object;
    - (void)setImageWithURLOrString:(id)object
                    placeholderImage:(UIImage *)placeholderImage;
  2. Luis Ascorbe

    Added two methods

    Lascorbe authored
    - (void)setImageWithURLOrString:(id)object;
    - (void)setImageWithURLOrString:(id)object
                    placeholderImage:(UIImage *)placeholderImage;
This page is out of date. Refresh to see the latest.
20 AFNetworking/UIImageView+AFNetworking.h
View
@@ -35,6 +35,15 @@
/**
Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL, and sets it the request is finished. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
+
+ @discussion By default, URL requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
+
+ @param object The URL used for the image request (can be NSURL or NSString).
+ */
+- (void)setImageWithURLOrString:(id)object;
+
+/**
+ Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL, and sets it the request is finished. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
@discussion By default, URL requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
@@ -44,6 +53,17 @@
/**
Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
+
+ @param object The URL used for the image request (can be NSURL or NSString).
+ @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
+
+ @discussion By default, URL requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
+ */
+- (void)setImageWithURLOrString:(id)object
+ placeholderImage:(UIImage *)placeholderImage;
+
+/**
+ Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
@param url The URL used for the image request.
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
47 AFNetworking/UIImageView+AFNetworking.m
View
@@ -79,10 +79,57 @@ + (AFImageCache *)af_sharedImageCache {
#pragma mark -
+- (void)setImageWithURLOrString:(id)object
+{
+ if (object == nil) {
+ return;
+ }
+ if ([object isKindOfClass:[NSString class]]) {
+ if ([object isEqual:@""]) {
+ return;
+ }
+
+ NSURL *url = [NSURL URLWithString:[object stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
+ [self setImageWithURL:url];
+ }
+ else if ([object isKindOfClass:[NSURL class]]) {
+ [self setImageWithURL:object];
+ }
+ else
+ {
+ NSLog(@"%s Object of type '%@' not supported", __PRETTY_FUNCTION__, NSStringFromClass([object class]));
+ }
+}
+
- (void)setImageWithURL:(NSURL *)url {
[self setImageWithURL:url placeholderImage:nil];
}
+- (void)setImageWithURLOrString:(id)object
+ placeholderImage:(UIImage *)placeholderImage
+{
+ if (object == nil) {
+ self.image = placeholderImage;
+ return;
+ }
+ if ([object isKindOfClass:[NSString class]]) {
+ if ([object isEqual:@""]) {
+ self.image = placeholderImage;
+ return;
+ }
+
+ NSURL *url = [NSURL URLWithString:[object stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
+ [self setImageWithURL:url placeholderImage:placeholderImage];
+ }
+ else if ([object isKindOfClass:[NSURL class]]) {
+ [self setImageWithURL:object placeholderImage:placeholderImage];
+ }
+ else
+ {
+ NSLog(@"%s Object of type '%@' not supported", __PRETTY_FUNCTION__, NSStringFromClass([object class]));
+ }
+}
+
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage
{
Something went wrong with that request. Please try again.