iphone UIImage用法介绍

本文介绍 iOS 开发中 UIImage 的多种实用操作方法,包括如何从给定图片中截取特定区域的新图片、不同场景下对 UIImage 进行缩放的方法,以及根据指定尺寸和其他参数对 UIImage 进行综合缩放和旋转的技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iphone 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; 
            bounds.size.height = bounds.size.width / ratio; 
        } 
        else 
        { 
            bounds.size.height = maxHeight; 
             bounds.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; 
            bounds.size.height = bounds.size.width / ratio; 
        } 
        else 
        { 
            bounds.size.height = kMaxResolution; 
            bounds.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; 
            bounds.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; 
            bounds.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; 
            bounds.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; 
            bounds.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; 
 
电动汽车数据集:2025年3K+记录 真实电动汽车数据:特斯拉、宝马、日产车型,含2025年电池规格和销售数据 关于数据集 电动汽车数据集 这个合成数据集包含许多品牌和年份的电动汽车和插电式车型的记录,捕捉技术规格、性能、定价、制造来源、销售和安全相关属性。每一行代表由vehicle_ID标识的唯一车辆列表。 关键特性 覆盖范围:全球制造商和车型组合,包括纯电动汽车和插电式混合动力汽车。 范围:电池化学成分、容量、续航里程、充电标准和速度、价格、产地、自主水平、排放、安全等级、销售和保修。 时间跨度:模型跨度多年(包括传统和即将推出的)。 数据质量说明: 某些行可能缺少某些字段(空白)。 几个分类字段包含不同的、特定于供应商的值(例如,Charging_Type、Battery_Type)。 各列中的单位混合在一起;注意kWh、km、hr、USD、g/km和额定值。 列 列类型描述示例 Vehicle_ID整数每个车辆记录的唯一标识符。1 制造商分类汽车品牌或OEM。特斯拉 型号类别特定型号名称/变体。型号Y 与记录关联的年份整数模型。2024 电池_类型分类使用的电池化学/技术。磷酸铁锂 Battery_Capacity_kWh浮充电池标称容量,单位为千瓦时。75.0 Range_km整数表示充满电后的行驶里程(公里)。505 充电类型主要充电接口或功能。CCS、NACS、CHAdeMO、DCFC、V2G、V2H、V2L Charge_Time_hr浮动充电的大致时间(小时),上下文因充电方法而异。7.5 价格_USD浮动参考车辆价格(美元).85000.00 颜色类别主要外观颜色或饰面。午夜黑 制造国_制造类别车辆制造/组装的国家。美国 Autonomous_Level浮点自动化能力级别(例如0-5),可能包括子级别的小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值