shajem
shajem

Reputation: 2121

Displaying images to a View

I am going through the following tutorial. I need to get thumbnail images of a large image. According to the tutorial the following method does it.

CGImageRef MyCreateThumbnailImageFromData (NSData * data, int imageSize)

note: look at the tutorial if you need the full code for this method

My questions are;

1.) i have a URL http://www.myimage.com/smile.jpg , and i need to resize it to a thumbnail. I don't undestand what is the NSData * data parameter. All what i have is a String URL. So how can i pass my URL to this method programatically ?

2.) The above method returns a CGImageRef But i need a UIImage so i could add it to a UIIMageVIew and then display it in my project. So how can i use CGImageRef to display images in my project ?

3.) The images i am downloading are very big, 2MB or more. By making it to appear in a thumbnail size, will it reduce the time taken to load the image to the view ?

Upvotes: 1

Views: 213

Answers (2)

ader
ader

Reputation: 5393

NSData* theData = [NSData dataWithContentsOfURL:@"http://www.myimage.com/smile.jpg"];
UIImage *theImage = [[UIImage alloc] initWithData:data];

UIGraphicsBeginImageContext(CGSizeMake(128, 96));
[theImage drawInRect:CGRectMake(0.0, 0.0, 128, 96)];
UIImage *myThumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *myThumbnailView = [[UIImageView alloc] initWithImage:myThumbnail];

EDIT for asynchronous approach:

in your header file:

NSURLConnection* connection;
NSMutableData* data;

- (void)loadImageFromURL:(NSURL*)url;

in your implementation file:

NSURL *url = [NSURL URLWithString:@"http://www.myimage.com/smile.jpg"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

- (void)loadImageFromURL:(NSURL*)url {

    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:@selector(loadImageFromURL:) withObject:url waitUntilDone:NO];
        return;
    }
    if (connection!=nil) { [connection release]; }

    if (data!=nil) { [data release]; }

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

}

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:4096]; } 
    [data appendData:incrementalData];
}



- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

    [connection release];
    connection=nil;

    UIImage *theImage    = [[UIImage alloc] initWithData:data];

    UIGraphicsBeginImageContext(CGSizeMake(128, 96));
    [theImage drawInRect:CGRectMake(0.0, 0.0, 128, 96)];
    UIImage *myThumbnail = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView *myThumbnailView = [[UIImageView alloc] initWithImage:myThumbnail];

    [data release];
    data=nil;
}

Upvotes: 0

Richard J. Ross III
Richard J. Ross III

Reputation: 55533

Lets go through this, step by step:

1) An NSData object is a wrapper for an array of bytes, or char *. This is the raw bytes of the image you need.

2) A CGImageRef is CoreGraphics's way of representing an image, which can be converted to a UIImage with the selector +imageWithCGImage:. Generally speaking, you have more fine control over the image with a CGImageRef.

3) Converting these images to a thumbnail will not reduce the time it takes to download. The file must first be downloaded to memory before it is converted.

Example of how to use your function:

int myImageSize = .... // do what you need to to figure out the size of the image
UIImage *myImage = [UIImage imageWithCGImage:MyCreateThumbnailImageFromData([NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myimage.com/smile.jpg"]], myImageSize)];

This, however will block the user interface, consider using GCD or NSURLConnection instead of -dataWithContentsOfURL:.

EDIT: Example with GCD:

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    __block UIImage *myImage = [UIImage imageWithCGImage:MyCreateThumbnailImageFromData([NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myimage.com/smile.jpg"]], myImageSize)];

    [myImageView performSelectorOnMainThread:@selector(setImage:) withObject:myImage waitUntilDone:NO];
});

Upvotes: 3

Related Questions