iOS 图片处理方法(按比例缩放,指定宽度按比例缩放)

今天遇见的处理图片的问题,一张图片上下两个部分都有一个空白区域,就中间是图片。要求是不能让他显示上下    有空白问间距。这是测试提出来的问题,但是图片本身就是这个毛病。无奈,哥哥改。谁有好的方法推荐推荐推荐      啊!!!

   两个方法如下

   建议让这两个放在UIImage的延展里,方便以后使用。

    1:按比例缩放

    在这里你传入的cgsize就是你要放大的区域,或者就是说设置放大的区域部分

     

//按比例缩放,size 是你要把图显示到 多大区域 
+ (UIImage *) imageCompressFitSizeScale:(UIImage *)sourceImage targetSize:(CGSize)size{
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = size.width;
    CGFloat targetHeight = size.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    
    if(CGSizeEqualToSize(imageSize, size) == NO){
        
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
            
        }
        else{
            
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        if(widthFactor > heightFactor){
            
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }else if(widthFactor < heightFactor){
            
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(size);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil){
        NSLog(@"scale image fail");
    }
    
    UIGraphicsEndImageContext();
    return newImage;
}

   2:按比例缩放

 这个代码很有意思

//指定宽度按比例缩放
-(UIImage *) imageCompressForWidthScale:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{

    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = height / (width / targetWidth);
    CGSize size = CGSizeMake(targetWidth, targetHeight);
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);

    if(CGSizeEqualToSize(imageSize, size) == NO){

        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }
        else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        if(widthFactor > heightFactor){

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

        }else if(widthFactor < heightFactor){

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }

    UIGraphicsBeginImageContext(size);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    if(newImage == nil){

        NSLog(@"scale image fail");
    }
    UIGraphicsEndImageContext();
    return newImage;
}

调用

UIImage *headImage = [UIImage imageWithContentsOfFile:imagePath];

        

        UIImage * newHeadImage = [UIImage imageCompressForSize:headImage targetSize:CGSizeMake(200, 140)];

        

        if (newHeadImage==nil) {

            newHeadImage = [UIImage imageNamed:@"headIcon"];

        }

        self.headerImageView.image = newHeadImage;

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个用 C++ 实现的 BMP 图片的示例代码,供你参考: ```cpp #include <iostream> #include <fstream> #include <cstring> #include <cmath> using namespace std; // BMP 文件头结构体 #pragma pack(push, 1) struct BMPFileHeader { uint16_t signature; // 文件类型标志 uint32_t file_size; // 文件大小 uint16_t reserved1; // 保留字段1 uint16_t reserved2; // 保留字段2 uint32_t offset; // 数据偏移量 uint32_t header_size; // 信息头大小 int32_t width; // 图像宽度 int32_t height; // 图像高度 uint16_t planes; // 颜色平面数 uint16_t bpp; // 每个像素的位数 uint32_t compression; // 压类型 uint32_t data_size; // 数据大小 int32_t x_resolution; // 水平分辨率 int32_t y_resolution; // 垂直分辨率 uint32_t color_used; // 颜色数 uint32_t color_important; // 重要颜色数 }; #pragma pack(pop) // BMP 图像像素数据结构体 struct BMPImageData { uint8_t r; uint8_t g; uint8_t b; }; // BMP 图像 void scale_bmp_image(const char* input_file, const char* output_file, float scale) { BMPFileHeader header; BMPImageData* data = nullptr; // 读取 BMP 文件头和像素数据 ifstream fin(input_file, ios::binary); if (!fin.read(reinterpret_cast<char*>(&header), sizeof(header))) { cerr << "读取文件 " << input_file << " 失败!" << endl; return; } if (header.signature != 0x4D42) { cerr << "文件 " << input_file << " 不是 BMP 格式!" << endl; return; } data = new BMPImageData[header.width * header.height]; fin.read(reinterpret_cast<char*>(data), header.data_size); // 计算后的图像大小 int new_width = static_cast<int>(round(header.width * scale)); int new_height = static_cast<int>(round(header.height * scale)); // 分配后的像素数据内存 BMPImageData* new_data = new BMPImageData[new_width * new_height]; // BMP 图像 for (int y = 0; y < new_height; y++) { for (int x = 0; x < new_width; x++) { // 计算后的像素坐标 float src_x = static_cast<float>(x) / scale; float src_y = static_cast<float>(y) / scale; // 双线性插值计算后的像素值 int x1 = static_cast<int>(floor(src_x)); int y1 = static_cast<int>(floor(src_y)); int x2 = static_cast<int>(ceil(src_x)); int y2 = static_cast<int>(ceil(src_y)); float f1 = (x2 - src_x) * (y2 - src_y); float f2 = (src_x - x1) * (y2 - src_y); float f3 = (x2 - src_x) * (src_y - y1); float f4 = (src_x - x1) * (src_y - y1); BMPImageData& p1 = data[y1 * header.width + x1]; BMPImageData& p2 = data[y1 * header.width + x2]; BMPImageData& p3 = data[y2 * header.width + x1]; BMPImageData& p4 = data[y2 * header.width + x2]; BMPImageData& new_p = new_data[y * new_width + x]; new_p.r = static_cast<uint8_t>(round(p1.r * f1 + p2.r * f2 + p3.r * f3 + p4.r * f4)); new_p.g = static_cast<uint8_t>(round(p1.g * f1 + p2.g * f2 + p3.g * f3 + p4.g * f4)); new_p.b = static_cast<uint8_t>(round(p1.b * f1 + p2.b * f2 + p3.b * f3 + p4.b * f4)); } } // 更新 BMP 文件头信息 header.width = new_width; header.height = new_height; header.file_size = header.offset + new_width * new_height * sizeof(BMPImageData); header.data_size = new_width * new_height * sizeof(BMPImageData); // 写入后的 BMP 图像 ofstream fout(output_file, ios::binary); fout.write(reinterpret_cast<const char*>(&header), sizeof(header)); fout.write(reinterpret_cast<const char*>(new_data), header.data_size); // 释内存 delete[] data; delete[] new_data; cout << "文件 " << input_file << " 完成,输出到 " << output_file << "。" << endl; } int main(int argc, char* argv[]) { if (argc != 4) { cerr << "用法:" << argv[0] << " <输入文件> <输出文件> <比例>" << endl; return 1; } float scale = atof(argv[3]); if (scale <= 0) { cerr << "比例必须大于零!" << endl; return 1; } scale_bmp_image(argv[1], argv[2], scale); return 0; } ``` 这段代码实现了 BMP 图片功能,使用双线性插值算法进行。你可以通过命令行参数指定输入文件、输出文件和比例,例如: ```bash ./scale_bmp_image input.bmp output.bmp 0.5 ``` 这条命令会将输入文件 `input.bmp` 小一半,输出到文件 `output.bmp` 中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值