iOS 圖片處理

A訫飛Flyme發表於2017-12-20

這就是愛一個人的眼神吧!

本文主要列出簡單的圖片處理程式碼,如:壓縮圖形大小,裁剪圖片,新增文字水印,新增圖片水印,壓縮圖片大小並儲存。

//兩張圖片
slimage = [UIImage imageNamed:@"標準圖.png"];
lfimage = [UIImage imageNamed:@"水印圖.png"];

//壓縮圖形大小
[self scaleToSize:slimage size:CGSizeMake(100, 100)];
//裁剪圖片
[self cutImage:slimage withRect:CGRectMake(300, 300, 100, 100)];
//新增文字水印
[self addImage:slimage text:@"SamLi" withFont:[UIFont systemFontOfSize:40.0] withRect:CGRectMake(20,20, 200, 200)];
//新增圖片水印
[self addToImage:slimage image:lfimage withRect:CGRectMake(20,20, 200, 200)];

//壓縮圖片並儲存
[self zipImageData:slimage];
複製程式碼
//具體功能實現
#pragma mark -
//壓縮圖形
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size
{
    // 設定成為當前context
    UIGraphicsBeginImageContext(size);
    // 繪製改變大小的圖片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 從當前context中建立一個改變大小後的圖片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使當前的context出堆疊
    UIGraphicsEndImageContext();
    return scaledImage;
}

//截圖圖片
- (UIImage *)cutImage:(UIImage *)image withRect:(CGRect )rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage * img = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return img;
}

//壓縮圖片儲存
- (void)zipImageData:(UIImage *)image
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMddHHSSS"];
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    NSString *dateStr = [NSString stringWithFormat:@"%@.jpg",currentDateStr];
    
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:dateStr];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
    }
    NSData *imgData = UIImageJPEGRepresentation(image, 1);
    
    if([imgData writeToFile:path atomically:YES])
    {
        NSLog(@"saveSuccess");
    }
}
//加文字水印
- (UIImage *) addImage:(UIImage *)img text:(NSString *)mark withFont:(UIFont *)font withRect:(CGRect)rect
{
    int w = img.size.width;
    int h = img.size.height;
    
    UIGraphicsBeginImageContext(img.size);
    [[UIColor redColor] set];
    [img drawInRect:CGRectMake(0, 0, w, h)];
    
    if([[[UIDevice currentDevice]systemName]floatValue] >= 7.0)
    {
        //ios 7.0以上
        NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,[UIColor blueColor] ,NSForegroundColorAttributeName,nil];
        [mark drawInRect:rect withAttributes:dic];
    }
    else
    {
        //7.0及其以後都廢棄了,完全可以不寫這個判斷
        [mark drawInRect:rect withFont:font];
    }
    
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
}

//加圖片水印
- (UIImage *) addToImage:(UIImage *)img image:(UIImage *)newImage withRect:(CGRect)rect
{
    int w = img.size.width;
    int h = img.size.height;
    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0, w, h)];
    [newImage drawInRect:rect];
    
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return aimg;
}

複製程式碼

本來想放GitHub,不過看看程式碼都在這了,還傳個毛線?

END

相關文章