SDWebImageManager issue - iphone

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
delegate:self
options:0
success:^(UIImage *image, BOOL cached) {
_newsimage.alpha = 0.0;
[UIView transitionWithView:_newsimage
duration:3.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[_newsimage setImage:image];
_newsimage.alpha = 1.0;
} completion:NULL];
}failure:nil];
I am using this code to download images through SDWebImage library and I am geting this error
No visible #interface for 'SDWebImageManager' declares the selector 'downloadWithURL:delegate:options:success:failure:'

May be you are not use proper SDWebImage. Download SDWebImage and check .
Add "SDWebImage" folder into you project and check.
You can also this code to download the image:
[imageViewName setImageWithURL:[NSURL URLWithString:StrImageUrl] placeholderImage:[UIImage imageNamed:#"noimage.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
}];
Import #import "UIImageView+WebCache.h" in you viewcontroller where you use code.
Hope it will resolve your issue. :)

Related

SDWebImage multiple image download issue

I need images from URLs in my app and I also need to cache them for reuse.
Here is my code to download various images from various URLs:
__block UIActivityIndicatorView *activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[imgView addSubview:activityIndicator];
activityIndicator.center = CGPointMake(65, 65);
[activityIndicator startAnimating];
[imgView setImageWithURL:imageURL placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
if (error)
NSLog(#"%#", [error description]);
[activityIndicator removeFromSuperview];
activityIndicator = nil;
}];
At a time I fire 4 requests. Out of them, only 3 gets the actual image. Remaining 1 image does not download and activityIndicator keeps animating forever.
Is there something I am missing in above code? I have copied this from their own example that comes with the SDK.

How to get frame of image when using MKNetworkKit setImageFromURL?

I am trying to get the size of an image after it is downloaded using setImageFromURL:
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[UIImageView setDefaultEngine:appDelegate.imageCache];
[img setImageFromURL:[NSURL URLWithString:#"http://testurl.com/testimg.jpg"]];
//img.frame = CGRectMake (0, 0, img.image.size.width, img.image.size.height);
[self.view addSubview:img];
I'm using MKNetworkKit -- is there a way to add a completion handler to the setImageFromURL method?
You can apply your own completion block
Few comments, please use Grand Central Dispatch to improve code efficiency and multi-core utilization for this kind of situations.
Example:
- (void) setImageFromUrl:(NSString*)urlString {
[self setImageFromUrl:urlString completion:NULL];
}
- (void) setImageFromUrl:(NSString*)urlString
completion:(void (^)(void))completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Starting: %#", urlString);
UIImage *avatarImage = nil;
NSURL *url = [NSURL URLWithString:urlString];
NSData *responseData = [NSData dataWithContentsOfURL:url];
avatarImage = [UIImage imageWithData:responseData];
NSLog(#"Finishing: %#", urlString);
if (avatarImage) {
dispatch_async(dispatch_get_main_queue(), ^{
self.image = avatarImage;
//Assign the image to ur ImageView.
//get the image frame from ur ImageView.frame
});
dispatch_async(dispatch_get_main_queue(), completion);
}
else {
NSLog(#"-- impossible download: %#", urlString);
}
});
}
For more details , please visit iOS GCD magic
I was able to do it by adding this line:
self.frame = CGRectMake(0, 0, fetchedImage.size.width, fetchedImage.size.height);
to UIImageView+MKNetworkKitAdditions.m:
if(imageCacheEngine) {
self.imageFetchOperation = [imageCacheEngine imageAtURL:url
size:self.frame.size
completionHandler:^(UIImage *fetchedImage, NSURL *url, BOOL isInCache) {
self.frame = CGRectMake(0, 0, fetchedImage.size.width, fetchedImage.size.height);
[UIView transitionWithView:self.superview
duration:isInCache?kFromCacheAnimationDuration:kFreshLoadAnimationDuration
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.image = fetchedImage;
} completion:nil];
//[[NSNotificationCenter defaultCenter] postNotificationName:KW_IMAGE_LOADED object:nil];
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
DLog(#"%#", error);
}];
} else {
DLog(#"No default engine found and imageCacheEngine parameter is null")
}

iOS set timeout for url request in SDWebImage

I'm using SDWebImage library for caching image in my iOS app. Now when loading image from server I'm showing an activity indicator on the imageview. I'm using the following code
__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = self.serviceImageView.center;
activityIndicator.hidesWhenStopped = YES;
[activityIndicator startAnimating];
[self.serviceImageView addSubview:activityIndicator];
[self.serviceImageView setImageWithURL:[NSURL URLWithString:[self.service valueForKey:#"image"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[activityIndicator removeFromSuperview];
}];
It works fine generally. But for some imageurls it didn't load the image and so it never comes to completed block & the activity indicator is spinning infinite.
So is there any way to set a timeout so that after a certain amount of time it gives me an error or something like that so I can stop the activity indicator.
It's bit argent. Please help.
You should check if the URL is valid, because when you pass nil to setImageWithURL the completion block won't be called.
NSURL* url = [NSURL URLWithString:[self.service valueForKey:#"image"]];
if (url)
[self.serviceImageView setImageWithURL:url placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[activityIndicator removeFromSuperview];
}];
I

SDWebImage corrupt images

I'm using SDWebImage (2.7.3 framework) and I receive corrupt images, I can't understand exactly the problem. If is the code (memory...)
(notes:
I get the same error using the SDWebImage project instead of the framework.
I'm implementing "autorelease" and other kinds of memory management.
This problem arises on devices (iPad), but not in the simulator)
__block CALayer *layerCover = [[CALayer alloc] init];
layerCover.frame = CGRectMake(3, 3, COVER_WIDTH_IPAD_SMALL, COVER_HEIGHT_IPAD_SMALL);
[btn.layer addSublayer:layerCover];
[_scroll addSubview:btn];
[btn release];
//request or load Vods Images
[[SDWebImageManager sharedManager] downloadWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#M", vod.cover]]
delegate:self options:SDWebImageProgressiveDownload success:^(UIImage *image, BOOL cached) {
if (image) {
layerCover.contents = (id)image.CGImage;
}
[layerCover release];
} failure:^(NSError *error) {
[layerCover release];
}];
//another kind
UIImage * imageTv = [UIImage imageNamed:#"bgDefaultTvImage.png"];
UIImageView * bgTvImage = [[UIImageView alloc] initWithFrame:CGRectMake(startX, 20, imageTv.size.width, imageTv.size.height)];
[bgTvImage setImage:imageTv];
CGFloat sizeWithIcon = imageTv.size.width;
CGFloat sizeHeightIcon = imageTv.size.height;
__block UIImageView * bgImageicon = [[UIImageView alloc] initWithFrame:CGRectMake((bgTvImage.frame.size.width-sizeWithIcon)/2,
(bgTvImage.frame.size.height-sizeHeightIcon)/2,
sizeWithIcon,
sizeHeightIcon)];
bgImageicon.contentMode = UIViewContentModeScaleAspectFit;
[bgTvImage addSubview:bgImageicon];
[tvTopView addSubview:bgTvImage];
/*
* Request ProgramImage
*/
[[SDWebImageManager sharedManager] downloadWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#&width=300",program.ProgramImage.imageURL]]
delegate:self options:SDWebImageProgressiveDownload success:^(UIImage *image, BOOL cached) {
if (image) {
iconCanal = image;
[bgImageicon setImage:iconCanal];
}
[bgImageicon release];
}failure:^(NSError *error) {
[bgImageicon release];
}];
Xcode logs:
<Error>: ImageIO: JPEG Corrupt JPEG data: bad Huffman code
<Error>: ImageIO: JPEG Corrupt JPEG data: premature end of data segment
It seems to be the problem of SDWebImageProgressiveDownload flag. Try to disable it, for example like this:
[[SDWebImageManager sharedManager] downloadWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#&width=300",program.ProgramImage.imageURL]]
delegate:self options:0 success:^(UIImage *image, BOOL cached) {
if (image) {
iconCanal = image;
[bgImageicon setImage:iconCanal];
}
[bgImageicon release];
}failure:^(NSError *error) {
[bgImageicon release];
}];

AVCamDemo: completionHandler code not hit

I am streamlining the AVCamDemo project so that I can experiment with capturing only still images.
Below is the new code for captureStillImage() method:
- (void) captureStillImage
{
AVCaptureConnection *stillImageConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
if ([stillImageConnection isVideoOrientationSupported]) {
[stillImageConnection setVideoOrientation:orientation];
}
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
NSLog(#"In the completionHandler block");
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self writeImageFile:image];
[image release];
}
if ([[self delegate] respondsToSelector:#selector(captureManagerStillImageCaptured:)]) {
[[self delegate] captureManagerStillImageCaptured:self];
}
}];
NSLog(#"exiting....");
}
In my testing, I have found that sometimes, the NSLog statement in the block is not executed....and therefore the stillImage file does not get saved.
Am I running into timing issues? If so, how do I address them?
Thanks, and regards.
Sam.