Schlagwort-Archive: UIImage

Objective-C: convert UIImage to greyscale

- (UIImage *)convertImageToGrayScale:(UIImage *)image
{
	// Create image rectangle with current image width/height
	CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

	// Grayscale color space
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

	// Create bitmap content with current image size and grayscale colorspace
	CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

	// Draw image into current context, with specified rectangle
	// using previously defined context (with grayscale colorspace)
	CGContextDrawImage(context, imageRect, [image CGImage]);

	// Create bitmap image info from pixel data in current context
	CGImageRef imageRef = CGBitmapContextCreateImage(context);

	// Create a new UIImage object  
	UIImage *newImage = [UIImage imageWithCGImage:imageRef];

	// Release colorspace, context and bitmap information
	CGColorSpaceRelease(colorSpace);
	CGContextRelease(context);
	CFRelease(imageRef);

	// Return the new grayscale image
	return newImage;
}

Objective-C: resize UIImage proportional

//the following is appropriate call to do this if img is the UIImage instance.
//img=[img scaleProportionalToSize:CGSizeMake(320, 480)];

@interface UIImage (UIImageFunctions)
	- (UIImage *) scaleToSize: (CGSize)size;
	- (UIImage *) scaleProportionalToSize: (CGSize)size;
@end
@implementation UIImage (UIImageFunctions)

- (UIImage *) scaleToSize: (CGSize)size
{
	// Scalling selected image to targeted size
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
	CGContextClearRect(context, CGRectMake(0, 0, size.width, size.height));

	if(self.imageOrientation == UIImageOrientationRight)
	{
		CGContextRotateCTM(context, -M_PI_2);
		CGContextTranslateCTM(context, -size.height, 0.0f);
		CGContextDrawImage(context, CGRectMake(0, 0, size.height, size.width), self.CGImage);
	}
	else
		CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), self.CGImage);

	CGImageRef scaledImage=CGBitmapContextCreateImage(context);

	CGColorSpaceRelease(colorSpace);
	CGContextRelease(context);

	UIImage *image = [UIImage imageWithCGImage: scaledImage];

	CGImageRelease(scaledImage);

	return image;
}

- (UIImage *) scaleProportionalToSize: (CGSize)size1
{
	if(self.size.width>self.size.height)
	{
		NSLog(@"LandScape");
		size1=CGSizeMake((self.size.width/self.size.height)*size1.height,size1.height);
	}
	else
	{
		NSLog(@"Potrait");
		size1=CGSizeMake(size1.width,(self.size.height/self.size.width)*size1.width);
	}

	return [self scaleToSize:size1];
}

@end

Objective-C: load/create UIImage from URL and resize

NSString* imageURL = [NSString stringWithFormat: @"http://theimageurl.com/?id=%@", [[resultsEntries objectAtIndex:0] objectForKey: @"image_large"]];
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData];

// resize image
CGSize newSize = CGSizeMake(100, 100);
UIGraphicsBeginImageContext( newSize );// a CGSize that has the size you want
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

//image is the original UIImage
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

imageHeight = image.size.height;
[imageMain setImage:newImage];
[imageData release];
[image release];

Objective-C: create UIImage from URL

NSString* imageURL = @"http://theurl.com/image.gif";
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];

UIImage* image = [[UIImage alloc] initWithData:imageData];
[imageView setImage:image];
[imageData release];
[image release];

//or nested version

UIImage* myImage = [UIImage imageWithData: 
	[NSData dataWithContentsOfURL: 
	[NSURL URLWithString: @"http://www.mydomain.com/image.png"]]];
[myImageView setImage:myImage];
[myImage release];

Objective-C: UIImage rounded corner examples

// UIImage+RoundedCorner.m
// Created by Trevor Harmon on 9/20/09.
// Free for personal or commercial use, with or without modification.
// No warranty is expressed or implied.

#import "UIImage+RoundedCorner.h"
#import "UIImage+Alpha.h"

// Private helper methods
@interface UIImage ()
- (void)addRoundedRectToPath:(CGRect)rect context:(CGContextRef)context ovalWidth:(CGFloat)ovalWidth ovalHeight:(CGFloat)ovalHeight;
@end

@implementation UIImage (RoundedCorner)

// Creates a copy of this image with rounded corners
// If borderSize is non-zero, a transparent border of the given size will also be added
// Original author: Björn Sållarp. Used with permission. See: http://blog.sallarp.com/iphone-uiimage-round-corners/
- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize {
	// If the image does not have an alpha layer, add one
	UIImage *image = [self imageWithAlpha];

	// Build a context that's the same dimensions as the new size
	CGContextRef context = CGBitmapContextCreate(NULL,
					image.size.width,
					image.size.height,
					CGImageGetBitsPerComponent(image.CGImage),
					0,
					CGImageGetColorSpace(image.CGImage),
					CGImageGetBitmapInfo(image.CGImage));

	// Create a clipping path with rounded corners
	CGContextBeginPath(context);
	[self addRoundedRectToPath:CGRectMake(borderSize, borderSize, image.size.width - borderSize * 2, image.size.height - borderSize * 2)
					context:context
					ovalWidth:cornerSize
					ovalHeight:cornerSize];
	CGContextClosePath(context);
	CGContextClip(context);

	// Draw the image to the context; the clipping path will make anything outside the rounded rect transparent
	CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);

	// Create a CGImage from the context
	CGImageRef clippedImage = CGBitmapContextCreateImage(context);
	CGContextRelease(context);

	// Create a UIImage from the CGImage
	UIImage *roundedImage = [UIImage imageWithCGImage:clippedImage];
	CGImageRelease(clippedImage);

	return roundedImage;
}

#pragma mark -
#pragma mark Private helper methods

// Adds a rectangular path to the given context and rounds its corners by the given extents
// Original author: Björn Sållarp. Used with permission. See: http://blog.sallarp.com/iphone-uiimage-round-corners/
- (void)addRoundedRectToPath:(CGRect)rect context:(CGContextRef)context ovalWidth:(CGFloat)ovalWidth ovalHeight:(CGFloat)ovalHeight {
	if (ovalWidth == 0 || ovalHeight == 0) {
		CGContextAddRect(context, rect);
		return;
	}
	CGContextSaveGState(context);
	CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
	CGContextScaleCTM(context, ovalWidth, ovalHeight);
	CGFloat fw = CGRectGetWidth(rect) / ovalWidth;
	CGFloat fh = CGRectGetHeight(rect) / ovalHeight;
	CGContextMoveToPoint(context, fw, fh/2);
	CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
	CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
	CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
	CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
	CGContextClosePath(context);
	CGContextRestoreGState(context);
}

@end

 

void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight);
{
	float fw, fh;
	if (ovalWidth == 0 || ovalHeight == 0) {
		CGContextAddRect(context, rect);
		return;
	}
	CGContextSaveGState(context);
	CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
	CGContextScaleCTM (context, ovalWidth, ovalHeight);
	fw = CGRectGetWidth (rect) / ovalWidth;
	fh = CGRectGetHeight (rect) / ovalHeight;
	CGContextMoveToPoint(context, fw, fh/2);
	CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
	CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
	CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
	CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
	CGContextClosePath(context);
	CGContextRestoreGState(context);
}

- (UIImage *)roundCornersOfImage:(UIImage *)source;
{
	int w = source.size.width;
	int h = source.size.height;

	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

	CGContextBeginPath(context);
	CGRect rect = CGRectMake(0, 0, w, h);
	addRoundedRectToPath(context, rect, 5, 5);
	CGContextClosePath(context);
	CGContextClip(context);

	CGContextDrawImage(context, CGRectMake(0, 0, w, h), source.CGImage);

	CGImageRef imageMasked = CGBitmapContextCreateImage(context);
	CGContextRelease(context);
	CGColorSpaceRelease(colorSpace);

	return [UIImage imageWithCGImage:imageMasked];    
}

Objective-C: crop image

// Create the image from a png file
UIImage *image = [UIImage imageNamed:@"prgBinary.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

// Get size of current image
CGSize size = [image size];

// Frame location in view to show original image
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];

// Create rectangle that represents a cropped image  
// from the middle of the existing image
CGRect rect = CGRectMake(size.width / 4, size.height / 4, (size.width / 2), (size.height / 2));

// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(0, 200, (size.width / 2), (size.height / 2))];
[[self view] addSubview:imageView];
[imageView release];

Objective-C: resize and mask an image

- (UIImage*) maskImage:(UIImage *)image {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

UIImage *maskImage = [UIImage imageNamed:@"mask.png"];
CGImageRef maskImageRef = [maskImage CGImage];

// create a bitmap graphics context the size of the image
CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

if (mainViewContentContext==NULL)
	return NULL;

CGFloat ratio = 0;

ratio = maskImage.size.width/ image.size.width;

if(ratio * image.size.height < maskImage.size.height) {
	ratio = maskImage.size.height/ image.size.height;
} 

CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
CGRect rect2  = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};

CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);

// Create CGImageRef of the main view bitmap content, and then
// release that bitmap context
CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);

UIImage *theImage = [UIImage imageWithCGImage:newImage];

CGImageRelease(newImage);

// return the image
return theImage;
}