https://github.com/lukeredpath/lrremoteimage
A really simple class to handle remote images
https://github.com/lukeredpath/lrremoteimage
Last synced: 10 months ago
JSON representation
A really simple class to handle remote images
- Host: GitHub
- URL: https://github.com/lukeredpath/lrremoteimage
- Owner: lukeredpath
- Created: 2011-07-20T14:40:01.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2011-07-20T15:03:03.000Z (almost 15 years ago)
- Last Synced: 2023-04-09T05:47:33.545Z (about 3 years ago)
- Language: Objective-C
- Homepage:
- Size: 125 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LRRemoteImage - really simple remote image handling
This class provides a very simple means of working with remote images.
It is written using ARC, although support for non-ARC builds will be forthcoming.
Example usage:
UIImageView *imageView = [self createImageView];
LRRemoteImage *remoteImage = [LRRemoteImage imageWithURL:[NSURL URLWithString:@"http://www.example.com/someimage.jpg"]];
imageView.image = remoteImage.image;
if (imageView.image == nil) {
[remoteImage fetchWithQueue:[NSOperationQueue mainQueue] completionHandler:^(UIImage *image, NSError *error) {
imageView.image = image;
if (error) {
// handle error
}
}];
}
All loaded images will be cached, using the URL as the cache key, using NSCache. This means that subsequent requests for the image after a fetchWithQueue:completionHandler: call will return the image, as will calls to a completely different LRRemoteImage instance for the same URL (the cache is shared across all LRRemoteImage instances).
Because NSCache is used, it will automatically be flushed in response to memory warnings.
## UIImageView category
The basic pattern above is encapsulated in a simple category on UIImageView. That means you can simply do the following:
UIImageView *imageView = [self createImageView];
LRRemoteImage *remoteImage = [LRRemoteImage imageWithURL:[NSURL URLWithString:@"http://www.example.com/someimage.jpg"]];
[imageView setRemoteImage:remoteImage errorHandler:^(NSError *error) {
// handle error
}];
Or, if you aren't worried about errors:
...
[imageView setRemoteImage:remoteImage];
The category will use the main NSOperationQueue for all fetches; if you need more control over this, you should use the LRRemoteImage API directly.
## License
This code is licensed under the MIT license.
Copyright (c) 2011 Luke Redpath
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.