iPhone处理图片(UIImage扩展类) 自动适应frame大小方法
来源:互联网 发布:阿里购买域名之后 编辑:程序博客网 时间:2024/05/16 03:40
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"图片地址"]]; UIImage *tempImg = [UIImage imageWithData:imgData];UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, 60, 80)];img.image = tempImg;//[img initWithImage:tempImg]; 用这个不行,为什么?[tempImg rescaleImageToSize:CGSizeMake(60, 80)];[self.view addSubView: img];
下面这个方法 可以改变图片大小 自适应,自己传参数吧
- (UIImage *)rescaleImageToSize:(CGSize)size {CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);UIGraphicsBeginImageContext(rect.size);[self drawInRect:rect]; // scales image to rectUIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return resImage;}
- (UIImage*)imageByScalingAndCroppin
- (UIImage*)transformWidth:(CGFloat)width
+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并图片
+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect; //裁剪部分图片
+ (void)imageSavedToPhotosAlbum:(UIImage *)image
didFinishSavingWithError
UIImageExtras
关于图片缩放的线程安全和非线程安全操作.
非线程安全的操作只能在主线程中进行操作,对于大图片的处理肯定会消耗大量的时间,如下面的方法
方法
+
{
// Create a graphics image context
UIGraphicsBeginImageCont
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage*
// End the context
UIGraphicsEndImageContex
// Return the new image.
return
}
此方法很简单,
方法
+
{
CGFloat
CGFloat
CGImageRef
CGBitmapInfo
CGColorSpaceRef
if
bitmapInfo
}
CGContextRef
if
bitmap
}
bitmap
}
if
CGContextRotateCTM
CGContextTranslateCTM
}
CGContextRotateCTM
CGContextTranslateCTM
}
// NOTHING
}
CGContextTranslateCTM
CGContextRotateCTM
}
CGContextDrawImage(bitmap,
CGImageRef
UIImage*
CGContextRelease(bitmap);
CGImageRelease(ref);
return
}
这种方法的好处是它是线程安全,加上它负责的
如何调整和保持长宽比
它是非常类似于上述,方法,它看起来像这样:
+
{
CGSize
CGFloat
CGFloat
CGFloat
CGFloat
CGFloat
CGFloat
CGFloat
CGPoint
if
CGFloat
CGFloat
if
scaleFactor
}
else
scaleFactor
}
scaledWidth
scaledHeight
// center the image
if
thumbnailPoint.y
}
else
thumbnailPoint.x
}
}
CGImageRef
CGBitmapInfo
CGColorSpaceRef
if
bitmapInfo
}
CGContextRef
if
bitmap
}
bitmap
}
// In the right or left cases, we need to switch scaledWidth and scaledHeight,
// and also the thumbnail point
if
thumbnailPoint
CGFloat
scaledWidth
scaledHeight
CGContextRotateCTM
CGContextTranslateCTM
}
thumbnailPoint
CGFloat
scaledWidth
scaledHeight
CGContextRotateCTM
CGContextTranslateCTM
}
// NOTHING
}
CGContextTranslateCTM
CGContextRotateCTM
}
CGContextDrawImage(bitmap,
CGImageRef
UIImage*
CGContextRelease(bitmap);
CGImageRelease(ref);
return
}
- iPhone处理图片(UIImage扩展类) 自动适应frame大小方法
- iPhone处理图片(UIImage扩展类) 自动适应frame大小方法
- iPhone处理图片(UIImage扩展类) 自动适应frame大小方法
- 图片自动适应大小
- 图片自动适应大小
- CSS图片自动适应大小
- 下载图片,大小自动适应
- 用css让图片自动适应大小
- 用css让图片自动适应大小
- 用css让图片自动适应大小
- 让图片自动适应框的大小
- 图片自动适应容器大小的JS
- excel插入图片自动适应表格大小
- frame高度自动适应
- 37-给UIImage,NSString添加扩展方法(根据内容拉伸图片,根据内容计算文本大小)
- iOS UIImage类扩展(按照位置和大小截图图片中部分图片)
- iOS 图片压缩UIImage方法扩展
- iOS 图片压缩UIImage方法扩展
- ftp自动上传下载
- 无法在当前安全上下文下访问数据库
- C++ 程序员自信心曲线图
- 中间件技术的概念和分类
- System.Web.UI.ScriptManager”的方法“get_EnableCdn”没有实现。
- iPhone处理图片(UIImage扩展类) 自动适应frame大小方法
- 多线程
- 三个很有用的sqlserver中的处理多个以分隔符分隔的参数的函数
- xhEditor for .net (支持上传,远程抓图)
- ubuntu crontab 编辑器
- Rails 3.1 安装及启动报错解决方案若干
- Big-Endian, Little-Endian和字节码对齐方式(下)
- Xcode Bate 4.2如何设置svn
- SVN 用户名密码的保存位置