作者: 傲骨天成科技 | 来源:发表于2017-06-24 15:48 被阅读6次

// 尺寸变换

  • (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize {

    // 创建图形要变化的大小的上下文
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    // 绘制图形的位置和大小
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    // 图形获取图片从当前图片的上下文
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    // 结束图形绘制
    UIGraphicsEndImageContext();

    return reSizeImage;
    }

// 等比率缩放

  • (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
    }

// 截图

  • (UIImage *)captureView:(UIView *)view {
    // 获得截图view的fram
    CGRect rect = view.frame;
    // 图形开始图片的上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获得当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 呈现图形当前的上下文到目标view的layer层中
    [view.layer renderInContext:context];

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
    }

// 储存图片, 这里分为储存在app的文件里和储存到手机的图库里

// 1.储存到app的文件里

  • (void)saveImageToAppFileWithImage:(UIImage *)image path:(NSString *)path{
    //把要处理的图片, 以image.png名称存到app home下的Documents目录里
    // NSString *pathStr = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
    [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
    }

// 2.储存图片到手机相册中(必须真机使用,模拟器无法使用)

  • (void)saveImageToPhotoAlbum:(UIImage*)savedImage {

    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
    // 保存图片成功与否的提示

  • (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo

{
NSString *msg = nil ;
if(error != NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功" ;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
message:msg
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}

相关文章

网友评论

    本文标题:

    本文链接:https://www.haomeiwen.com/subject/acekcxtx.html