}
- (UIImage*)transformWidth:(CGFloat)width //改变大小
height:(CGFloat)height {
CGFloat destW = width;
CGFloat destH = height;
CGFloat sourceW = width;
CGFloat sourceH = height;
CGImageRef imageRef = self.CGImage;
CGContextRef bitmap = CGBitmapContextCreate(NULL,
destW,
destH,
CGImageGetBitsPerComponent(imageRef),
4*destW,
CGImageGetColorSpace(imageRef),
(kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
CGContextDrawImage(bitmap, CGRectMake(0, 0, sourceW, sourceH), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
//result.imageOrientation = UIImageOrientationRight;
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 //合并图片
{+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect;//裁剪部分图片
Create a UIImage from a part of another UIImage
This requires a round-trip to Core Graphics land:
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
return newImage;
}
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
return newImage;
}
Save UIImage to Photo Album//保存图片到媒体库
This is just a one-liner:
UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), context);
UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), context);
And to know if the save was successful:
- ( void )imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:( void *)contextInfo //保存图片到媒体库{
NSString *message;
NSString *title;
if (!error) {
title = NSLocalizedString(@"SaveSuccessTitle" , @ "" );
message = NSLocalizedString(@"SaveSuccessMessage" , @ "" );
} else {
title = NSLocalizedString(@"SaveFailedTitle" , @ "" );
message = [error description];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"ButtonOK" , @ "" )
otherButtonTitles:nil];
[alert show];
[alert release];
}