Uiimageview, uiimage, and cgcontextref knowledge points

Source: Internet
Author: User

1.Uiimageview does not support Tile)

2.The image in the resource must be in lower case, and the simulator may not be case sensitive,However, in a real machine.

[Uiimage imagenamed: @ ""]; case sensitive on devices

3.Uiview has no background image attribute and background color attribute. You can use addsubview (backgroundimage) to set the background image. We recommend that you set the background color.

4.[Uiimage imagenamed: @ ""]; it isCache featuresOf

+ (Uiimage *) imagewithcontentsoffile :( nsstring *)Path; // does not cache the image object.

Imagenamed Document explanation:

This method looksIn the system caches for an image object
With the specified name and returns that object if it exists. if a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

On a device running IOS 4 or later, the behavior is identical if the device's screen has a scale1.0. If the screen has a scale2.0, This method first searches for an image file with the same filename with@2x
Suffix appended to it. For example, if the file's name isbutton, It first searchesbutton@2x. If it finds a 2x, it loads that image and setsscaleProperty of the returnedUIImageObject2.0.
Otherwise, it loads the unmodified filename and setsscaleProperty1.0.

On iOS 4 and later, the name of the file is not required to specify the filename extension. Prior to iOS 4, you must specify the filename extension.

After multiple operations, the application may frequently experience a memory warning, resulting in automatic exit. After positioning, it was found that [uiimage imagenamed: @ ""] The allocated images were not released. The previous information obtained from the official reference should be [uiimage imagenamed: @ ""]. The allocated image system will be placed in the cache. The cache management rules are not clearly described.From this perspective, [uiimage imagenamed:] is only suitable for reading textures in the UI, while some large resource files should try to avoid using this interface.

5.-(Uiimage *) stretchableimagewithleftcapwidth :( nsinteger) leftcapwidth topcapheight :( nsinteger) topcapheight

Specifies the part that is not scaled in two directions.

Corresponding rightcapwidth = image. Size. Width-(image. leftcapwidth + 1 );

Bottingapheight = image. Size. Height-(image. topcapheight + 1 );

The pixels between leftcapwidth and rightcapwidth, topcapheight, and botw.apheight are tiled to achieve scaling.

In addition, uiview. contentstretch does not seem to work here.

Note: When using this function, you must obtain its return value. And that method cocould return NilIf the base image is less than 5 pixels wide or 5 pixels tall
Since it needs the 4 pixels for the caps + 1 pixel to stretch.

Example: Set the custom return button on the navigation bar

- (void)setNavigationBackButton{    const int KLeftWidth=25;    UIImage* image=[[UIImage imageNamed:@"back.png"] stretchableImageWithLeftCapWidth:KLeftWidth topCapHeight:0];    UIImage* hightlightedImage=[[UIImage imageNamed:@"back_pressed.png"] stretchableImageWithLeftCapWidth:KLeftWidth topCapHeight:0];    NSString* text=[[[self.navigationController viewControllers] objectAtIndex:0] navigationItem].title;    UIFont* font=[Utility fontWithSize:12];    CGSize stringSize = [text sizeWithFont:font];    CGFloat textWidth = stringSize.width+KLeftWidth;        UIButton* button=[UIViewUtil createUIButtonWithImage:image                                       hightlightedImage:hightlightedImage                                             selectedImage:nil                                                   title:text                                              titleColor:color(0x33,0x33,0x33)                                                    posX:0 posY:0];    button.titleLabel.font=[Utility fontWithSize:12];    CGRect rect=button.frame;    rect.size.width=textWidth;    button.frame=rect;    UIBarButtonItem* buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];    [button addTarget:self action:@selector(popViewControllerAnimated) forControlEvents:UIControlEventTouchUpInside];    [[self navigationItem] setLeftBarButtonItem:buttonItem];    [buttonItem release];    [button release];    }

6.Supports built-in animation and video sequence.

    imageView.animationImages = imageArray;    imageView.animationDuration = 0.75f;    [self.view addSubview:imageView];    [imageView startAnimating];

7.For uiimageview, you must set self. userinteractionenabled = yes to support interaction.

8.Image Construction Methods:

Uigraphicsbeginimagecontext (cgsizemake (side_length, side_length); // create a new image context cgcontextref context = uigraphicsgetcurrentcontext ();... uiimage * theimage = uigraphicsgetimagefromcurrentimagecontext (); // convert the context into a uiimage object. Uigraphicsendimagecontext (); Return theimage;

9.Dynamic images, such as GIF, are not supported by the iPhone. uiwebview is used instead of GIF.

   (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{            NSString* referenceURL=[info objectForKey:UIImagePickerControllerReferenceURL];        NSLog(@"%@",referenceURL);  //assets-library://asset/asset.JPG?id=1000000001&ext=JPG                ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];...}#import <AssetsLibrary/ALAssetsLibrary.h>#import <AssetsLibrary/ALAssetRepresentation.h>//http://stackoverflow.com/questions/5187251/ios-select-a-gif-from-the-photo-library-convert-to-nsdata-for-use-in-multipart

10.Save image to album

-(Void) savetophotosalbum {uiimage * image = nil; nsobject * OBJ = ..; if ([OBJ iskindofclass: [uiimage class]) {image = (uiimage *) OBJ;} else {image = [uiimage imagewithdata :( nsdata *) OBJ];} if (image) {uiimagewritetosavedphotosalbum (image, self, @ selector (image: didfinishsavingwitherror: contextinfo :), nil);} else {// @ "failed to save ";}} # If 1-(void) image :( uiimage *) image didfinishsavingwitherror :( nserror *) Error contextinfo :( void *) contextinfo {If (error = nil) // @ "saved successfully"; else // @ "failed to save";} # endif

11.Emoji

Emoji is a one-character set developed in Japan. It is integrated in iOS and can be determined by programming to enable or disable this function.

It has been tested and can be displayed in uilabel, uibutton, and uiwebview, using Unicode codes such as "\ ue415.

Http://pukupi.com/post/1964/

You can use the draw method of nsstring to draw an expression on uiimage:

CGSize size = [text sizeWithFont:aFont];if (UIGraphicsBeginImageContextWithOptions != NULL){   UIGraphicsBeginImageContextWithOptions(size,NO,0.0);}else{   UIGraphicsBeginImageContext(size);}[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:aFont];UIImage *image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();    return image;

IOS emoji is enabled for programming. The test is not successful.
Http://blog.csdn.net/favormm/article/details/6774899

Solution to the emoji display box in ios5.1

Emoji cannot be displayed normally on some ios5.1 devices.The reason is that emoji on ios4 uses Softbank encoding. After ios5, emoji is put into unicode6.0, which may cause some incompatibility of the old encoding.
The solution is to use new encoding on ios5 and old encoding on ios4 and below.
Because some ios5.1 can be displayed normally, some may not. According to our test, all of 5.x uses the new encoding, And all of 4.x and below use the old encoding.
Apple's own conversion table: Unicode new encoding on the left of the http://opensource.apple.com/source/ICU/ICU-461.13/icuSources/data/translit/Any_SoftbankSMS.txt, Softbank old encoding on the right, Please convert it yourself

Http://www.cocoachina.com/bbs/read.php? Tid = 96847 & page = 1

12.After taking a picture with uiimagepickercontroller, the image is rotated 90 degrees. This may not happen in 5.0.

# Pragma mark watermark-(void) imagepickercontroller :( uiimagepickercontroller *) picker syntax :( nsdictionary *) info {[picker Syntax: Yes]; nsstring * mediatype = [info objectforkey: encoding]; if ([mediatype isw.tostring :( nsstring *) kuttypeimage]) // @ "public. image "{uiimage * image = [info objectforkey: uiimagepi Ckercontrolleroriginalimage]; uiimageorientation imageorientation = image. imageorientation; If (imageorientation! = Uiimageorientationup) {// the original image can be displayed based on the camera angle, but the uiimage cannot be determined. Therefore, the obtained image will turn left 90 degrees. // The following figure shows the uigraphicsbeginimagecontext (image. size); [Image drawinrect: cgrectmake (0, 0, image. size. width, image. size. height)]; iportraitimageview. image = resize (); uigraphicsendimagecontext (); // adjust the image angle }}// set the uiimagepickercontroller * pickercontroller = [[uiimagepickercontroller alloc] init]; pickercontroller. delegate = self; pickercontroller. allowsediting = yes; // You can edit the selected image if (buttonindex = 0) {If ([uiimagepickercontroller issourcetypeavailable: uiimagepickercontrollersourcetypephotolibrary]) {pickercontroller. sourcetype = uiimagepickercontrollersourcetypephotolibrary;} else {If ([uiimagepickercontroller issourcetypeavailable: uiimagepickercontrollersourcetypecamera]) {pickercontroller. sourcetype = require ;}} [[uiviewutil handle: Self] presentmodalviewcontroller: pickercontroller animated: Yes]; [pickercontroller release];-(void) imagepickercontroller :( uiimagepickercontroller *) picker syntax :( nsdictionary *) info {[picker Syntax: Yes]; nsstring * mediatype = [info objectforkey: uiimagepickercontrollermediatype]; If ([mediatype is#tostring :( nsstring *) kuttypeimage]) // @ "public. image "{uiimage * image = [info objectforkey: uiimagepickercontrollereditedimage]; iportraitimageview. image = image ;}}test again later. The result is: uiimage * image = [info objectforkey: uiimagepickercontrolleroriginalimage]; uiimageorientation imageorientation = image. imageorientation; The imageorientation value is shown in the left column. When taking the photo, the Home Key is placed in the value of the right column. uiimageorientationright: uiimageorientationup right: uiimageorientationdown: Left uiimageorientationleft

If a black screen appears after taking the photo multiple times, check whether the printed content is warned, and check whether the previous picture has not been released.

13.The value of image. Size. Width of Nil may be uncertain. The value may be 0 on most machines, while some machines get an uncertain value.

Therefore, you must determine that if it is nil, the width value is 0.

14.When the image is filled with backgroundcolor, the transparent area on the image may become black.Backgroundcolor is paved with the content size.

 

15.In the Resource Directory, if icon.png 114x114 is present at the same time, icon@2x.png 114x114 pixels

[Image imagenamed: @ "icon.png"]; // The size is. The image is icon.png, And the pixel/point is 1: 1nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "icon" oftype: @ "PNG"]; [uiimage imagewithcontentsoffile: path]; // The size is. The image is icon.png, And the pixel/point is 1: 1 Path = [[nsbundle mainbundle] pathforresource: @ "icon @ 2x" oftype: @ "PNG"]; uiimage imagewithcontentsoffile: path]; // The size is. The image is icon@2x.png and the pixel/point is.

The results of imagewithcontentsoffile are the same as those in logs.

It looks like the @ 2x recognition in the file name is completed by the uiimage after finding the Image Based on the path, and it looks at the final image name, not necessarily in the path.

Data = [nsdata datawithcontentsoffile: path]; image = [uiimage imagewithdata: data=114 icon.pngand icon@2x.png. The size is and the pixel/point is.

16.Rounded Images

Http://www.cocoachina.com/bbs/read.php? Tid = 1757 & page = 1

17.Apply a filter to the image

Use the core graphic API to parse the image into a four-channel rgba bitmap and put it into the memory. Then there is an array in the memory, each of the four elements in the array is the rgba value (0-255) of a pixel on the image. Change the RGB value and write it back to regenerate.

Changing to a black-and-white image is to add the RGB value of each pixel to calculate the average value, and then write it back. For example, r = B = G = 100, which is gray. In the for loop, the RGB values of each pixel are changed to their respective average values, and the photos are black and white. If the image changes to a nostalgic image, the background color is yellow, that is, the ratio of RG is increased, and B remains unchanged, because the Red-Green match is yellow.
With the colormatrix concept in Android, it is important to adjust the RGB value of each pixel to a new value.

Http://www.cocoachina.com/bbs/read.php? Tid = 69525

Http://www.cnblogs.com/leon19870907/articles/1978065.html

18.Painting translucent rectangle
Cgcontextref CTX = uigraphicsgetcurrentcontext ();
Cgcontextsetfillcolorwithcolor (CTX, [[uicolor blackcolor] colorwithalphacomponent: 0.5]. cgcolor );
Cgcontextaddrect (CTX, cgrectmake (0, 0,320, 44 ));
Cgcontextclosepath (CTX );
Cgcontextdrawpath (CTX, kcgpathfill );

19.Gpuimage

Https://github.com/BradLarson/GPUImage

Compile gpuimage. xcodeproj in the framework directory, generate. A, and then compile an application under examples.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.