4

Is there any way of achieving the following that avoids using "initWithData" ? (Just in case you are curious, initWithData is getting my app flagged by Apple as using an illegal API sigh).

NSData * imageData = [NSData dataWithContentsOfURL : [NSURL URLWithString : [details image]]];
    picture = [[UIImage alloc] initWithData:imageData];

Many thanks,

Martin

1

4 Answers 4

6

if you want to get the image data,then initialize a UIImage using that data:

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://Serverurl/pic007.jpg"]];
cell.image = [UIImage imageWithData: imageData];
[imageData release];
Sign up to request clarification or add additional context in comments.

Comments

5

First of all, you should do this asynchronously so that your thread won't block. Here is the code for the class:

@implementation AsyncImageView

+ (void)initialize {
    [NSURLCache setSharedURLCache:[[SDURLCache alloc] initWithMemoryCapacity:0
                                                                diskCapacity:10*1024*1024
                                                                    diskPath:[SDURLCache defaultCachePath]]];
}

- (void)setImageFromURL:(NSURL *)url{
    /* Put activity indicator */
    if(!activityIndicator) {
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        CGRect frame = [activityIndicator frame];
        frame.origin.x = (self.frame.size.width - frame.size.width)/2;
        frame.origin.y = (self.frame.size.height - frame.size.height)/2;
        activityIndicator.tag = 9999;
        activityIndicator.frame = frame;
        [self addSubview:activityIndicator];
        [activityIndicator startAnimating];
    }

    /* Cancel previous request */
    if(fetchImageConnection) {
        [fetchImageConnection cancel];
    }
    [imageData release];

    /* Start new request */
    NSURLRequest *req = [NSURLRequest requestWithURL:url
                                         cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                     timeoutInterval:30];
    imageData = [NSMutableData new];
    fetchImageConnection = [NSURLConnection connectionWithRequest:req
                                                         delegate:self];
    [fetchImageConnection retain];
}
- (void)setImageFromDisk:(UIImage *)img {
    self.image = img;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [imageData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if(connection == fetchImageConnection) {
        self.image = [UIImage imageWithData:imageData];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"imageDownloaded" object:self];

        [activityIndicator removeFromSuperview];

        [imageData release];
        [activityIndicator release];

        activityIndicator = nil;
        imageData = nil;
        fetchImageConnection = nil;
    }
    [connection release];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];
    NSLog(@"error: %@", error);
}
@end

2 Comments

Am I the only one who balked at "SDURLCache"?
URL could be on disk
2

Try this code:

NSString *imgString = @"https://www.lockated.com/system/attachfiles/documents/000/002/489/original/ZPMHaJUSjAGnUrVuOmbqoExRMryvcySVOIkJQMivnAntvpmpYd.jpg?1501833649";

NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgString]];

accountImageView.image = [UIImage imageWithData: imageData]; // accountImageView is imageView

Comments

1
NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://Serverurl/pic007.jpg"]];
 self.image=nil;
 UIImage *img = [[UIImage alloc] initWithData:receivedData ];
 self.image = img;
 [img release];

I hope this code will help you!!

1 Comment

that's pretty much the exact code he has in the question - and he's specifically asking for a workaround to initWithData:.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.