iphone iPhone开发应用UIImage图片对象操作
1原文摘自:http://mobile.51cto.com/iphone-284346.htm
iPhone开发应用UIImage图片对象操作是本文要介绍的内容,主要来学习UIImage图片对象操作,内容不多,主要是以代码实现图片对象的操作。
1、根据给定得图片,从其指定区域截取一张新得图片。
- -(UIImage *)getImageFromImage{
- //大图bigImage
- //定义myImageRect,截图的区域
- CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
- UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];
- CGImageRef imageRef = bigImage.CGImage;
- CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
- CGSize size;
- size.width = 57.0;
- size.height = 57.0;
- UIGraphicsBeginImageContext(size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextDrawImage(context, myImageRect, subImageRef);
- UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
- UIGraphicsEndImageContext();
- return smallImage;
- }
2、针对UIImage得一些常用缩放得方法:
- -(UIImage*)resizedImage1:(UIImage*)inImage inRect:(CGRect)thumbRect {
- // Creates a bitmap-based graphics context and makes it the current context.
- UIGraphicsBeginImageContext(thumbRect.size);
- [inImage drawInRect:thumbRect];
- return UIGraphicsGetImageFromCurrentImageContext();
- }
- -(UIImage*)resizedImage2:(UIImage*)inImage inRect:(CGRect)thumbRect {
- CGImageRef imageRef = [inImage CGImage];
- CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
- /* There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
- see Supported Pixel Formats in the Quartz 2D Programming Guide
- Creating a Bitmap Graphics Context section
- only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst,
- kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,and kCGImageAlphaPremultipliedLast,
- with a few other oddball image kinds are supported
- The images on input here are likely to be png or jpeg files*/
- if (alphaInfo == kCGImageAlphaNone)
- alphaInfo = kCGImageAlphaNoneSkipLast;
- // Build a bitmap context that's the size of the thumbRect
- CGFloat bytesPerRow;
- if( thumbRect.size.width > thumbRect.size.height ) {
- bytesPerRow = 4 * thumbRect.size.width;
- } else {
- bytesPerRow = 4 * thumbRect.size.height;
- }
- CGContextRef bitmap = CGBitmapContextCreate(NULL,
- thumbRect.size.width, // width
- thumbRect.size.height, // height
- 8, //CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
- bytesPerRow, //4 * thumbRect.size.width, // rowbytes
- CGImageGetColorSpace(imageRef),
- alphaInfo
- );
- // Draw into the context, this scales the image
- CGContextDrawImage(bitmap, thumbRect, imageRef);
- // Get an image from the context and a UIImage
- CGImageRef ref = CGBitmapContextCreateImage(bitmap);
- UIImage* result = [UIImage imageWithCGImage:ref];
- CGContextRelease(bitmap); // ok if NULL
- CGImageRelease(ref);
- return result;
- }
- - (UIImage *)scaleImage:(UIImage *) image maxWidth:(float) maxWidth maxHeight:(float) maxHeight
- {
- CGImageRef imgRef = image.CGImage;
- CGFloat width = CGImageGetWidth(imgRef);
- CGFloat height = CGImageGetHeight(imgRef);
- if (width <= maxWidth && height <= maxHeight)
- {
- return image;
- }
- CGAffineTransform transform = CGAffineTransformIdentity;
- CGRect bounds = CGRectMake(0, 0, width, height);
- if (width > maxWidth || height > maxHeight)
- {
- CGFloat ratio = width/height;
- if (ratio > 1)
- {
- bounds.size.width = maxWidth;
- boundsbounds.size.height = bounds.size.width / ratio;
- }
- else
- {
- bounds.size.height = maxHeight;
- boundsbounds.size.width = bounds.size.height * ratio;
- }
- }
- CGFloat scaleRatio = bounds.size.width / width;
- UIGraphicsBeginImageContext(bounds.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextScaleCTM(context, scaleRatio, -scaleRatio);
- CGContextTranslateCTM(context, 0, -height);
- CGContextConcatCTM(context, transform);
- CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
- UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return imageCopy;
- }
3、其他缩放UIImage的size,有需要的可以看看
- -(UIImage*) scaleAndRotateImage:(UIImage*)photoimage:(CGFloat)bounds_width:(CGFloat)bounds_height
- {
- //int kMaxResolution = 300;
- CGImageRef imgRef =photoimage.CGImage;
- CGFloat width = CGImageGetWidth(imgRef);
- CGFloat height = CGImageGetHeight(imgRef);
- CGAffineTransform transform = CGAffineTransformIdentity;
- CGRect bounds = CGRectMake(0, 0, width, height);
- /*if (width > kMaxResolution || height > kMaxResolution)
- {
- CGFloat ratio = width/height;
- if (ratio > 1)
- {
- bounds.size.width = kMaxResolution;
- boundsbounds.size.height = bounds.size.width / ratio;
- }
- else
- {
- bounds.size.height = kMaxResolution;
- boundsbounds.size.width = bounds.size.height * ratio;
- }
- }*/
- bounds.size.width = bounds_width;
- bounds.size.height = bounds_height;
- CGFloat scaleRatio = bounds.size.width / width;
- CGFloat scaleRatioheight = bounds.size.height / height;
- CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
- CGFloat boundHeight;
- UIImageOrientation orient =photoimage.imageOrientation;
- switch(orient)
- {
- case UIImageOrientationUp: //EXIF = 1
- transform = CGAffineTransformIdentity;
- break;
- case UIImageOrientationUpMirrored: //EXIF = 2
- transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
- transform = CGAffineTransformScale(transform, -1.0, 1.0);
- break;
- case UIImageOrientationDown: //EXIF = 3
- transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
- transform = CGAffineTransformRotate(transform, M_PI);
- break;
- case UIImageOrientationDownMirrored: //EXIF = 4
- transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
- transform = CGAffineTransformScale(transform, 1.0, -1.0);
- break;
- case UIImageOrientationLeftMirrored: //EXIF = 5
- boundHeight = bounds.size.height;
- boundsbounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
- transform = CGAffineTransformScale(transform, -1.0, 1.0);
- transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
- break;
- case UIImageOrientationLeft: //EXIF = 6
- boundHeight = bounds.size.height;
- boundsbounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
- transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
- break;
- case UIImageOrientationRightMirrored: //EXIF = 7
- boundHeight = bounds.size.height;
- boundsbounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeScale(-1.0, 1.0);
- transform = CGAffineTransformRotate(transform, M_PI / 2.0);
- break;
- case UIImageOrientationRight: //EXIF = 8
- boundHeight = bounds.size.height;
- boundsbounds.size.height = bounds.size.width;
- bounds.size.width = boundHeight;
- transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
- transform = CGAffineTransformRotate(transform, M_PI / 2.0);
- break;
- default:
- [NSException raise:NSInternalInconsistencyException format:@"Invalid?image?orientation"];
- break;
- }
- UIGraphicsBeginImageContext(bounds.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft)
- {
- CGContextScaleCTM(context, -scaleRatio, scaleRatioheight);
- CGContextTranslateCTM(context, -height, 0);
- }
- else
- {
- CGContextScaleCTM(context, scaleRatio, -scaleRatioheight);
- CGContextTranslateCTM(context, 0, -height);
- }
- CGContextConcatCTM(context, transform);
- CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
- UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return imageCopy;
- }
小结:iPhone开发应用UIImage图片对象操作的内容介绍完了,希望通过本文的学习能对你有所帮助!