UIImage and NSCoding

THURSDAY, MARCH 5, 2009

UIImage and NSCoding

Here is a category to conform UIImage to NSCoding so you can archive it. This is untested - I hacked this up a few minutes in response to a question posed on Twitter. Please
let me know if it needs any corrections:

UIImage-NSCoding.h

//
//  UIImage-NSCoding.h

#import <Foundation/Foundation.h>

@interface UIImageNSCoding <NSCoding>
- (id)initWithCoder:(NSCoder *)decoder;
- (void)encodeWithCoder:(NSCoder *)encoder;
@end

UIImage-NSCoding.m

//
//  UIImage-NSCoding.m

#import "UIImage-NSCoding.h"
#define kEncodingKey        @"UIImage"

@implementation UIImage(NSCoding)
- (id)initWithCoder:(NSCoder *)decoder
{
    if ((self = [super init]))
    {
        NSData *data = [decoder decodeObjectForKey:kEncodingKey];
        self = [self initWithData:data];
    }

    return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
    NSData *data = UIImagePNGRepresentation(self);
    [encoder encodeObject:data forKey:kEncodingKey];
}
@end

This is also checked into Google Code if you prefer to grab it from there.

POSTED BY JEFF LAMARCHE AT 9:27
PM

LABELS: CATEGORIESUIIMAGE

时间: 2025-01-23 22:59:15

UIImage and NSCoding的相关文章

ios-下载和设置UIImage不在主线程中

问题描述 下载和设置UIImage不在主线程中 从web服务器中添加一个图片.怎么设置它不在主线程上?我想要视图先加载,然后图片加载.这样用户在体验时不会感到很慢. 就是不知道怎么加到代码中. NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:mainImageUrl]]; UIImage *image = [ [UIImage alloc] initWithData:imageData]; UIView

ios-能不能将NSDecimal值转换为UIImage

问题描述 能不能将NSDecimal值转换为UIImage 需要发送一些数据和UIImage到服务器中. 客户端会返回响应. 但是UIImage发送后客户端返回的就是这样: FaceImage":[255,216,255,224,0,16,74,70,73,70,0,1,1,1,1,44,1,44,0,0,255,219,0,67,0,8,6,6,7,6,5,8,7,7,7,9,9,8,10,12,20,13,12,11,11,12,25,18,19,15,20,29,26,31,30,29,2

UIImage扩展

// // UIImage+Scale.h // CloudShopping // // Created by sixiaobo on 14-7-8. // Copyright (c) 2014年 com.Uni2uni. All rights reserved. // #import <UIKit/UIKit.h> /*! * @brief * @author huangyibiao */ @interface UIImage (Scale) /*! * @brief 根据指定的Rect来截

IOS中图片(UIImage)拉伸技巧

IOS中图片拉伸技巧与方法总结 一.了解几个图像拉伸的函数和方法 1.直接拉伸法 简单暴力,却是最最常用的方法,直接将图片设置为ImageView的image属性,图片便会随UIImageView对象的大小做自动拉伸.这种拉伸的方法有一个致命的缺陷,它会使图像发生失真与形变. 2.像素点的拉伸 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHe

iOS UIImage 转NSString

项目中可能会有这样的需求,比如图片上传服务器时一般都会转成NSString再上传 那么问题来了,图片如何转为NSString ,已经被转为NSString的图片如何再转成图片 其实很简单,给大家两个方法就ok [html] view plain copy //图片转字符串   -(NSString *)UIImageToBase64Str:(UIImage *) image   {       NSData *data = UIImageJPEGRepresentation(image, 1.0

iOS应用开发中对UIImage进行截取和缩放的方法详解_IOS

截取UIImage指定大小区域最近遇到这样的需求:从服务器获取到一张照片,只需要显示他的左半部分,或者中间部分等等.也就是截取UIImage指定大小区域. UIImage扩展: 我的解决方案是对UIImage进行扩展.通过CGImageRef和CGImage完成截取,调用的方法是:CGImageCreateWithImageInRect.扩展类叫UIImage+Crop,具体代码如下: UIImage+Crop.h #import <UIKit/UIKit.h> typedef NS_ENUM

uiimage-cv::mat转换到UIImage时恢复图像

问题描述 cv::mat转换到UIImage时恢复图像 使用下面的函数转换cv::mat到UIImage. -(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat { NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()]; CGColorSpaceRef colorSpace; if (cvMat.elemSize() == 1) { color

UIView转UIImage(截屏,UIGraphicsBeginImageContextWithOptions函数的使用)

UIView转UIImage(截屏,UIGraphicsBeginImageContextWithOptions函数的使用)   2012-11-06 13:56:59|  分类: iphone_dev_note|举报|字号 订阅 // 需要转成image的view:captureAndSaveView UIGraphicsBeginImageContextWithOptions(captureAndSaveView.frame.size, NO, 1.0);  //NO,YES 控制是否透明

uiimage-EMChatImage 怎么转成UIImage?

问题描述 EMChatImage 怎么转成UIImage? 最近在做即时通讯,发送图片消息要显示在imageview上遇到如上问题,请大神们指教 解决方案 self.imgView.image =[UIImage imageWithContentsOfFile:((EMImageMessageBody *)messageBody).thumbnailLocalPath];