美文网首页首页投稿(暂停使用,暂停投稿)
iOS 截屏的方法,拉伸图片的方法,压缩图片的方法

iOS 截屏的方法,拉伸图片的方法,压缩图片的方法

作者: IOSMan | 来源:发表于2016-07-24 07:22 被阅读326次

    本次更新三个常用的方法,全部写成了 UIImage 的分类方法

    截屏

    //截屏
    + (UIImage *)snapScreen {
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, [UIScreen mainScreen].scale);
        [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:false];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image;
    }
    

    压缩图片

    //压缩图片
    + (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;
    }
    

    拉伸图片

    //拉伸图片
    + (UIImage *)resizableImageName:(NSString *)imageName {
        
        UIImage *oldBackgroundImage = [Utility imageNamedWithFileName:imageName];
        CGFloat top = oldBackgroundImage.size.height * 0.5;
        CGFloat left = oldBackgroundImage.size.width * 0.5;
        CGFloat bottom = oldBackgroundImage.size.height * 0.5;
        CGFloat right = oldBackgroundImage.size.width * 0.5;
        
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
        UIImageResizingMode mode = UIImageResizingModeStretch;
        UIImage *newBackgroundImage = [oldBackgroundImage resizableImageWithCapInsets:edgeInsets resizingMode:mode];
        
        return newBackgroundImage;
    }
    

    相关文章

      网友评论

        本文标题:iOS 截屏的方法,拉伸图片的方法,压缩图片的方法

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