伊的家怎么样有谁知道道iPhone怎么鍳IF图片

iPhone处理图片(UIImage扩展类) 自动适应frame大小方法 - BradyChen - 博客园
posts - 139, comments - 113, trackbacks - 0, articles - 1
NSData&*imgData = [NSData&dataWithContentsOfURL:[NSURL&URLWithString:@"图片地址"]];
UIImage&*tempImg = [UIImage&imageWithData:imgData];
UIImageView&*img = [[UIImageView&alloc]&initWithFrame:CGRectMake(15,&20,&60,&80)];
img.image&= tempI
//[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 rect
UIImage&*resImage =&UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return&resI
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetS &&//图片缩放裁剪
- (UIImage*)transformWidth:(CGFloat)width &&&&&height:(CGFloat) //改变大小
+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并图片
+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect) //裁剪部分图片
+ (void)imageSavedToPhotosAlbum:(UIImage *)image
didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextI &//保存图片到媒体库
关于图片缩放的线程安全和非线程安全操作.
非线程安全的操作只能在主线程中进行操作,对于大图片的处理肯定会消耗大量的时间,如下面的方法
方法&1:&使用&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(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage*&newImage&=&UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return&newImage;
此方法很简单,&但是,这种方法不是线程安全的情况下.
方法&2:&使用&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);
bitmap&=&CGBitmapContextCreate(NULL,&targetHeight,&targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef),&colorSpaceInfo,&bitmapInfo);
if&(sourceImage.imageOrientation&==&UIImageOrientationLeft)&{
CGContextRotateCTM&(bitmap,&radians(90));
CGContextTranslateCTM&(bitmap,&0,&-targetHeight);
}&else&if&(sourceImage.imageOrientation&==UIImageOrientationRight)&{
CGContextRotateCTM&(bitmap,&radians(-90));
CGContextTranslateCTM&(bitmap,&-targetWidth,&0);
}&else&if&(sourceImage.imageOrientation&==&UIImageOrientationUp)&{
// NOTHING
}&else&if&(sourceImage.imageOrientation&==&UIImageOrientationDown){
CGContextTranslateCTM&(bitmap,&targetWidth,&targetHeight);
CGContextRotateCTM&(bitmap,&radians(-180.));
CGContextDrawImage(bitmap,&CGRectMake(0,&0,&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
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);
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(90));
CGContextTranslateCTM&(bitmap,&0,&-targetHeight);
}&else&if&(sourceImage.imageOrientation&==UIImageOrientationRight)&{
thumbnailPoint&=&CGPointMake(thumbnailPoint.y,&thumbnailPoint.x);
CGFloat&oldScaledWidth&=&scaledWidth;
scaledWidth&=&scaledHeight;
scaledHeight&=&oldScaledWidth;
CGContextRotateCTM&(bitmap,&radians(-90));
CGContextTranslateCTM&(bitmap,&-targetWidth,&0);
}&else&if&(sourceImage.imageOrientation&==&UIImageOrientationUp)&{
// NOTHING
}&else&if&(sourceImage.imageOrientation&==&UIImageOrientationDown){
CGContextTranslateCTM&(bitmap,&targetWidth,&targetHeight);
CGContextRotateCTM&(bitmap,&radians(-180.));
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;iPhone是否能够帮助HEIF彻底取代JPEG
来源:驱动之家
  HEIF 是苹果用来替代 JPEG 的新照片格式,是的,它非常有潜力取代 JPEG ,因为目前 iPhone 是世界上最受欢迎、最常用的手机,或者也可以说是移动相机,而在 iOS 11 中,大多数的 iPhone 将会从 JPEG 切换到 HEIF 上。  那么 HEIF 能为我们带来什么不同之处呢?  HEIF 全名是 High Efficiency Image File Format(高效率图档格式),是由动态图像专家组(MPEG)在 2013 年推出的新格式,它可以用更少的容量来存储画质更好的照片,在连拍、动图和色彩变化不大的照片上效果尤其明显。  这意味着,我们可以在 iPhone 上储存更多的照片,或者换一种方式,你可以保留你所有的照片,同时在你的 iPhone 上释放大量的存储进空间。  HEIF 比 JPEG 在 1992 年诞生时要好得多,这在一定程度上是因为如今的计算机变得更加强大,它们可以用更复杂的方式粉碎文件,而且几乎可以立即完成。苹果最新的理念是为 HVEC 提供内置的硬件支持,这意味着图像可以被快速编码和解码,而无需强调系统或电池。  HEIF 与 JPEG 相比,它的优点真的不少。首先,它不是文件格式,而是文件的容器。JPEG 是单个图像,但 HEIF 可以是单个图像或图像序列。  这使得 HEIF 很适合苹果的 Live Photos ,这也让它成为 GIF 的潜在替代品。HEIF 支持的图像颜色最多为 16 位,而 JPEG 只有 8 位。在实际操作中,HEIF 可以捕捉由相机 10 位颜色输出所提供的所有扩展颜色范围。  HEIF 也很适合编辑。HEIF 图像可以在不改变图像或重新保存图像的情况下旋转和裁剪。  现在,我们生活在一个 JPEG 世界里。为了适应这一功能,iOS 11 将把 HEIF 图片转换为 JPEG 文件,比如分享给非 iOS 设备,或者将图片传递给不支持HEIF 的应用等等,所以作为一个用户,你什么也不会注意到,所有的工作都将在幕后完成。  但是慢慢的你会注意到它的好处,照片会占用更少的空间,同时看起来更好。他们将会在你的 iCloud 图片库上上传和下载的速度加快。而且,如果世界其他地方都采用了 HEIF 来代替 JPEG ,那么整个网络将会运行得更快。  值得注意的是,HEIF 并不是一种苹果专有的技术,它比 JPEG 更重要。就像有无数的人认为 AAC 是一种专有的苹果文件格式,其实真实情况并不是这样的。  JPEG 已经存在了四分之一个世纪,它就像文件格式一样。但或许,它会迎来退休的时刻,而 HEIF 看起来将会是一个合适的继承者,由于庞大的 iOS 用户基数,它很有可能会篡夺 JPEG 的地位,不知道,这样的未来会不会到来呢。

我要回帖

更多关于 2017有谁知道hs网站 的文章

 

随机推荐