iswear_wxp/UIImage to NSData ( iPhone)
1
2
3
4
5
6
7
|
PNG格式的图片:
UIImage *image.....;
NSData *imageData = UIImagePNGRepresentation(image,1.0);
JEPG格式的图片:
UIImage *image.....;
NSData *imageData = UIImageJEPGRepresentation(image);
|
rabc/UIImage from URL ( Objective C)
1
2
3
4
|
NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
imageView.image = [UIImage imageWithData:data];
[data release];
|
Load data from url and send it to an UIImage
gerhardsletten/Create a UIImage from url ( Objective C)
1
2
3
4
|
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO]
|
tenveer/take a screen shot of the view and save it in photo library ( Objective C)
1
2
3
4
5
6
|
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@
"board.png"
atomically:YES];
|
take a screen shot of the view passed in "UIGraphicsBeginImageContext"
espinallab/Post a UIImage to the web ( Objective C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
- (IBAction)uploadImage {
/*
turning the image into a NSData object
getting the image back out of the UIImageView
setting the quality to 90
*/
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
// setting up the URL to post to
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@
"POST"
];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@
"---------------------------14737809831466499882746641449"
];
NSString *contentType = [NSString stringWithFormat:@
"multipart/form-data; boundary=%@"
,boundary];
[request addValue:contentType forHTTPHeaderField: @
"Content-Type"
];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
}
Here is the php function to grab the uploaded file:
$uploaddir =
'./uploads/'
;
$file = basename($_FILES[
'userfile'
][
'name'
]);
$uploadfile = $uploaddir . $file;
if
(move_uploaded_file($_FILES[
'userfile'
][
'tmp_name'
], $uploadfile)) {
}
|
So here is something a lot of people have been wondering to do in the forums. How do I take a UIImage or any image and post it to a website. So this will go over how to do that.
There are two ways to tackle this issue. One we can base64 encode the file and post is normally inside a XML or JSON instance or we can simulate a normal HTML post. This tutorial will go over the HTML post.
dio/UIImage from IplImage ( Objective C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
// NOTE You should convert color mode as RGB before passing to this function
- (UIImage *)UIImageFromIplImage:(IplImage *)image {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Allocating the buffer for CGImage
NSData *data =
[NSData dataWithBytes:image->imageData length:image->imageSize];
CGDataProviderRef provider =
CGDataProviderCreateWithCFData((CFDataRef)data);
// Creating CGImage from chunk of IplImage
CGImageRef imageRef = CGImageCreate(
image->width, image->height,
image->depth, image->depth * image->nChannels, image->widthStep,
colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault,
provider, NULL,
false
, kCGRenderingIntentDefault
);
// Getting UIImage from CGImage
UIImage *ret = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return
ret;
}
|
suibhne/Populate UIImageView from URL ( Objective C)
1
2
3
4
5
6
7
|
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[imageView setImage:image];
[imageData release];
[image release];
|
Populate a UIImageView with an image taken from a URL.
svenito/Resize UIImage ( iPhone)
1
2
3
4
5
6
7
8
9
|
- (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext
();
UIGraphicsEndImageContext();
return scaledImage;
}
|
Scales a UIImage to the given size
aahmed753/UIImage resize ( C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
{
CGImageRef imageRef = [inImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if
(alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width,
// width
thumbRect.size.height,
// height
CGImageGetBitsPerComponent(imageRef),
// really needs to always be 8
4 * thumbRect.size.width,
// rowbytes
CGImageGetColorSpace(imageRef),
alphaInfo
);
// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);
// Get an image from the context and a UIImage
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
// ok if NULL
CGImageRelease(ref);
return
result;
}
|
vaishnavi/Take any view and make a UIImage out of it. ( Objective C)
1
2
3
4
5
6
7
8
9
10
11
12
|
- (UIImage*)imageFromView{
UIImage *image;
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return
image;
}
|
Any view and it's subviews will be "flattened" into a UIImage that you can display or save to disk.
aviddv1/Straighten and Scale UIImage ( Objective C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
- (UIImage*)straightenAndScaleImage:(UIImage *)theFullImage andTheMaxDimension:(
int
)maxDimension
{
CGImageRef img = [theFullImage CGImage];
CGFloat width = CGImageGetWidth(img);
CGFloat height = CGImageGetHeight(img);
CGRect bounds = CGRectMake(0, 0, width, height);
CGSize size = bounds.size;
if
(width > maxDimension || height > maxDimension) {
CGFloat ratio = width/height;
if
(ratio > 1.0f) {
size.width = maxDimension;
size.height = size.width / ratio;
}
else
{
size.height = maxDimension;
size.width = size.height * ratio;
}
}
CGFloat scale = size.width/width;
CGAffineTransform transform = orientationTransformForImage(theFullImage, &size);
//CGAffineTransform transform = orientationTransformForImage(theFullImage, &size);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Flip
UIImageOrientation orientation = [theFullImage imageOrientation];
if
(orientation == UIImageOrientationRight || orientation == UIImageOrientationLeft) {
CGContextScaleCTM(context, -scale, scale);
CGContextTranslateCTM(context, -height, 0);
}
else
{
CGContextScaleCTM(context, scale, -scale);
CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, bounds, img);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return
newImage;
}
CGAffineTransform orientationTransformForImage(UIImage *image, CGSize *newSize) {
CGImageRef img = [image CGImage];
CGFloat width = CGImageGetWidth(img);
CGFloat height = CGImageGetHeight(img);
CGSize size = CGSizeMake(width, height);
CGAffineTransform transform = CGAffineTransformIdentity;
CGFloat origHeight = size.height;
UIImageOrientation orient = image.imageOrientation;
switch
(orient) {
/* EXIF 1 to 8 */
case
UIImageOrientationUp:
break
;
case
UIImageOrientationUpMirrored:
transform = CGAffineTransformMakeTranslation(width, 0.0f);
transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
break
;
case
UIImageOrientationDown:
transform = CGAffineTransformMakeTranslation(width, height);
transform = CGAffineTransformRotate(transform, M_PI);
break
;
case
UIImageOrientationDownMirrored:
transform = CGAffineTransformMakeTranslation(0.0f, height);
transform = CGAffineTransformScale(transform, 1.0f, -1.0f);
break
;
case
UIImageOrientationLeftMirrored:
size.height = size.width;
size.width = origHeight;
transform = CGAffineTransformMakeTranslation(height, width);
transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
transform = CGAffineTransformRotate(transform, 3.0f * M_PI / 2.0f);
break
;
case
UIImageOrientationLeft:
size.height = size.width;
size.width = origHeight;
transform = CGAffineTransformMakeTranslation(0.0f, width);
transform = CGAffineTransformRotate(transform, 3.0f * M_PI / 2.0f);
break
;
case
UIImageOrientationRightMirrored:
size.height = size.width;
size.width = origHeight;
transform = CGAffineTransformMakeScale(-1.0f, 1.0f);
transform = CGAffineTransformRotate(transform, M_PI / 2.0f);
break
;
case
UIImageOrientationRight:
size.height = size.width;
size.width = origHeight;
transform = CGAffineTransformMakeTranslation(height, 0.0f);
transform = CGAffineTransformRotate(transform, M_PI / 2.0f);
break
;
default
:
;
}
*newSize = size;
return
transform;
}
|
Usage: [self straightenAndScaleImage:imageView.image andTheMaxDimension:100];
dio/IplImage from UIImage ( Objective C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// NOTE you SHOULD cvReleaseImage() for the return value when end of the code.
- (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
// Getting CGImage from UIImage
CGImageRef imageRef = image.CGImage;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Creating temporal IplImage for drawing
IplImage *iplimage = cvCreateImage(
cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4
);
// Creating CGContext for temporal IplImage
CGContextRef contextRef = CGBitmapContextCreate(
iplimage->imageData, iplimage->width, iplimage->height,
iplimage->depth, iplimage->widthStep,
colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault
);
// Drawing CGImage to CGContext
CGContextDrawImage(
contextRef,
CGRectMake(0, 0, image.size.width, image.size.height),
imageRef
);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
// Creating result IplImage
IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
cvCvtColor(iplimage, ret, CV_RGBA2BGR);
cvReleaseImage(&iplimage);
return
ret;
}
|
oppey/UIImageView メモリリークとなることがある。 ( Objective C)
1
2
3
4
5
6
7
8
9
10
11
|
UIImageViewで画像を使うとき、下記のようにimageNamedを使って画像ファイルを読み込むとキャッシュされるので、メモリリークとなることがある。
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@
"Photo.png"
]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
代替策として、dataWithContentsOfFileを使うとキャッシュを使わずに画像を表示することができる。
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@
"Photo"
ofType:@
"png"
];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
|
suibhne/Add Image Border to UIImage ( Objective C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
- (UIImage*)imageWithBorderFromImage:(UIImage*)source;
{
CGSize size = [source size];
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 0.5, 1.0, 1.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return
testImg;
}
|
Adds a fine border to a UIImage.
shadevampire/draw to a UIImage with CoreGraphics ( Objective C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
CGFloat width, height;
// max 1024 due to Core Graphics limitations
UIImage *inputImage;
// input image to be composited over new image as example
// create a new bitmap image context
//
UIGraphicsBeginImageContext(CGSizeMake(width, height));
// get context
//
CGContextRef context = UIGraphicsGetCurrentContext();
// push context to make it current
// (need to do this manually because we are not drawing in a UIView)
//
UIGraphicsPushContext(context);
//
// [drawing code comes here- look at CGContext reference
// for available operations]
//
// this example draws the inputImage into the context
//
[inputImage drawInRect:CGRectMake(0, 0, width, height)];
// pop context
//
UIGraphicsPopContext();
// get a UIImage from the image context- enjoy!!!
//
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
// clean up drawing environment
//
UIGraphicsEndImageContext();
|
input: width, height and inputImage output: outputImag