UIImageView自适应图片大小
窗口大小获取: CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect CGRect rect = [ [UIScreenmainScreen]applicationFrame];//不包含状态栏的Rect UIImageView: 一 :圆角以及自适应图片大小 UIImage* image = [UIImage imageNamed:@"image.png"]; UIImageView* imageView = [[[UIImageView alloc] initWithImage:image] autorelease]; imageView.frame = CGRectMake(, , , ); imageView.layer.cornerRadius = ; imageView.layer.masksToBounds = YES; //自适应图片宽高比例 imageView1.contentMode = UIViewContentModeScaleAspectFit; 二 图片自适应UIImageView - (UIImage *)rescaleImageToSize:(CGSize)size { CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height); UIGraphicsBeginImageContext(rect.size); [self drawInRect:rect]; // scales image to rect UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resImage; } - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize; //图片缩放裁剪 - (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height; //改变大小 + (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并图片 + (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect; //裁剪部分图片 + (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo; //保存图片到媒体库 关于图片缩放的线程安全和非线程安全操作. 非线程安全的操作只能在主线程中进行操作,对于大图片的处理肯定会消耗大量的时间,如下面的方法 方法 : 使用 UIKit + (UIImage*)imageWithImage :( UIImage*)image scaledToSize :(CGSize)newSize;
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize); // Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(,,newSize.width,newSize.height)]; // Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context
UIGraphicsEndImageContext(); // Return the new image.
return newImage; } 此方法很简单, 但是,这种方法不是线程安全的情况下. 方法 : 使用 CoreGraphics + (UIImage*)imageWithImage :( UIImage*)sourceImage scaledToSize :(CGSize)newSize;
{ CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast; } CGContextRef bitmap; if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo) } else { bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians());
CGContextTranslateCTM (bitmap, , -targetHeight); } else if (sourceImage.imageOrientation ==UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-));
CGContextTranslateCTM (bitmap, -targetWidth, ); } else if (sourceImage.imageOrientation == UIImageOrientationUp) { // NOTHING } else if (sourceImage.imageOrientation == UIImageOrientationDown){ CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-.)); } CGContextDrawImage(bitmap, CGRectMake(, , targetWidth,targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
} 这种方法的好处是它是线程安全,加上它负责的 (使用正确的颜色空间和位图信息,处理图像方向) 的小东西,UIKit 版本不会。
如何调整和保持长宽比 (如 AspectFill 选项)?
它是非常类似于上述,方法,它看起来像这样: + (UIImage*)imageWithImage :( UIImage*)sourceImage scaledToSizeWithSameAspectRatio :( CGSize)targetSize;
{
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor) {
scaleFactor = widthFactor; // scale to fit height
}
else {
scaleFactor = heightFactor; // scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor < heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} // In the right or left cases, we need to switch scaledWidth and scaledHeight,
// and also the thumbnail point
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
CGFloat oldScaledWidth = scaledWidth;
scaledWidth = scaledHeight;
scaledHeight = oldScaledWidth;
CGContextRotateCTM (bitmap, radians());
CGContextTranslateCTM (bitmap, , -targetHeight);
} else if (sourceImage.imageOrientation ==UIImageOrientationRight) {
thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
CGFloat oldScaledWidth = scaledWidth;
scaledWidth = scaledHeight;
scaledHeight = oldScaledWidth;
CGContextRotateCTM (bitmap, radians(-));
CGContextTranslateCTM (bitmap, -targetWidth, );
} else if (sourceImage.imageOrientation == UIImageOrientationUp) { // NOTHING } else if (sourceImage.imageOrientation == UIImageOrientationDown){
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-.));
} CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x,thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
UIImageView自适应图片大小的更多相关文章
- iOS UIImageView自适应图片大小
窗口大小获取: CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect CGRect rect = [ [UIScre ...
- bootstrap 响应式图片自适应图片大小
<img src="..." class="img-responsive center-block" > 或者 $(window).load(fun ...
- UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图
/* 设置圆角,通过layer中的cornerRadius和masksToBounds即可. 自适应图片宽高比例.通过UIViewContentModeScaleAsp ...
- WebView加载HTML图片大小自适应与文章自动换行
http://www.brighttj.com/ios/ios-webview-load-html-image-adaptive.html 在很多App中都会使用到webview,尤其是在加载新闻内容 ...
- JS获取图片实际宽高及根据图片大小进行自适应
JS获取图片实际宽高,以及根据图片大小进行自适应 <img src="http://xxx.jpg" id="imgs" onload="ad ...
- android 图片大小适配,如何在不同屏幕上适配图片,如何设置可以自适应不同分辨率?
android 图片大小适配,如何在不同屏幕上适配图片,如何设置可以自适应不同分辨率? Android为了适应不同的分辨率,需要将不同的图片放到不同的drawable目录下,分辨率的匹配规则如下:dr ...
- pyqt5:图片自适应QLabel大小和图片移除
参考链接: https://www.e-learn.cn/content/qita/669569 图片自适应QLabel大小 # coding=utf- import sys from PyQt5.Q ...
- [jquery] 图片热区随图片大小自由缩放
在图片上直接画出带超级链接热区元素map和area相信大家并不陌生,Dreamweaver等网页制作软件都有直接在图片上绘制带超级链接的热区工具,但是直接绘制的热区是不能随着图片自适应放大和缩小的,现 ...
- Css Sprite 图片等比缩放图片大小
图片大小80*40,即每张图片大小40*40,如何以20*20显示图片?1. 首先看下如何以40*40显示第二张图片: 正常显示css代码 .sprite { background-image: ur ...
随机推荐
- java-猜数字
package com.jijy.circle; import java.util.Scanner; import java.util.Random; public class demo5 { pub ...
- Rhino学习教程——1.1
在Rhino的官网下载好Rhino5.0版本后(Rhino官网会提供下载方式,官网是http://www.xuexiniu.com),双击桌面快捷键,就会出现Rhino的界面(我已经自定义过界面了). ...
- Android直接用手机打包apk!
你没有看错,用手机浏览器访问Jenkins,就可以打包apk,并生成下载二维码,发送邮件通知测试人员下载,从此解放双手,告别打包测试.先上本人手机邮箱收到的打包成功通知效果图: 废话少说, ...
- Unity实现用户条款弹窗及登录
首先来看效果图... 1.先编辑页面 1)新建登录按钮,更名为Login 2)新建toggle,新建方式如下图 调整toggle的大小和位置到适当的范围. 3)新建同意用户条款按钮,步骤为新建UI-& ...
- LeetCode 98 验证二叉搜索树
题目: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是 ...
- C/C++(static)
出自:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777441.html 静态全局变量作用范围在一个文件内,程序开始时分配空间,结束 ...
- Python全栈day9习题
本内容主要为If条件语句和while循环的相关知识. 一.使用while循环输入1 2 3 4 5 6 8 9 10 i = 1 while i < 11: if i == 7: pass el ...
- python初学代码留个纪念
最简单的代码 if else if else 1.python中else if 用 elif表示 2.注释: 单行注释:##### 多行注释:''' ------''',"&q ...
- Ubuntu 修改sudoers之后无法用sudo怎么恢复
进入终端 键入 pkexec visudo 修改sudoer.temp 实例如下 ## This file MUST be edited with the 'visudo' command as ro ...
- CentOS7的网卡重启方法
1.centos6的网卡重启方法:service network restartcentos7的网卡重启方法:systemctl restart network 2.DNS配置文件:cat /etc/ ...