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

GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113
  • 1
    According to the iOS reference, initWithData: isn't a private API. http://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/initWithData: – makdad Apr 12 '11 at 09:26

4 Answers4

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];
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
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
rckoenes
  • 69,092
  • 8
  • 134
  • 166
Stefan Ticu
  • 2,093
  • 1
  • 12
  • 21
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
shim
  • 9,289
  • 12
  • 69
  • 108
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!!

Gypsa
  • 11,230
  • 6
  • 44
  • 82
  • 1
    that's pretty much the exact code he has in the question - and he's specifically asking for a workaround to initWithData:. – makdad Apr 12 '11 at 09:24