我试图捕捉我的UIView的图像,但当我称这种方法
- (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序在我的视图的DrawRect方法中输入并停留在这些行的循环中
[super drawRect:rect];
[curImage drawAtPoint:CGPointMake(0, 0)];
mid1 = midPoint(previousPoint1, previousPoint2);
mid2 = midPoint(currentPoint, previousPoint1);
context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
Run Code Online (Sandbox Code Playgroud)
这是我的drawRect方法
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
[curImage drawAtPoint:CGPointMake(0, 0)];
mid1 = midPoint(previousPoint1, previousPoint2);
mid2 = midPoint(currentPoint, previousPoint1);
context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextStrokePath(context);
}
Run Code Online (Sandbox Code Playgroud) 我将照片相册中的图像存储在我的应用文档目录中作为NSData,而不是在UITableView中显示它们.我的表现有问题.将图像保存到app目录需要花费几秒钟的时间,并且在将它们加载到TableView时遇到同样的问题.这是我存储图像的代码:
//Convert UIImage to NSData
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
//Save image to app documents directory
NSError *error;
[imageData writeToFile:fullImagePath options:NSDataWritingAtomic error:&error];
Run Code Online (Sandbox Code Playgroud)
这就是我将它们加载到UITableView中的方法:
NSData *imageData = [NSData dataWithContentsOfFile:path];
UIImage *myImage = [UIImage imageWithData:imageData];
cell.imageView.image = myImage;
Run Code Online (Sandbox Code Playgroud)
什么可能导致这些性能问题?是否有任何其他方式来存储和检索应用程序文档目录中的图像?
从以下源文件,我尝试加载图像并保存到Core数据,并在imageView中显示,但它不起作用你可以帮我修复它.
- (IBAction)save:(id)sender
{
NSLog(@"Telling the AddTrackingTVC Delegate that save was tapped on the AddTrackingTVC");
[self.delegate theSaveButtonOnTheAddPlantNoteTVCWasTapped:self];
MyPlant *myTracking = [NSEntityDescription insertNewObjectForEntityForName:@"MyPlant" inManagedObjectContext:self.managedObjectContext];
myTracking.name = nameTextField.text;
myTracking.detail = detailTextField.text;
myTracking.createdDate = [NSDate date];
//myTracking.photo = [NSData ];
[self.managedObjectContext save:nil]; // write data to database
[self.delegate theSaveButtonOnTheAddPlantNoteTVCWasTapped:self];
}
- (IBAction)takePicture:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// If our device has a camera, we want to take a picture, otherwise, we // just pick from photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) …Run Code Online (Sandbox Code Playgroud) 我试着创建一个框架和颜色的UIImage,但不知道正确的方法.
UIImage *aImage = [UIImage imageWithCIImage:[CIImage imageWithColor:[CIColor colorWithCGColor:[UIColor redColor].CGColor]]];
Run Code Online (Sandbox Code Playgroud)
以上行是否正确?如何创建图像大小?请帮忙!
问题:缩小图像的裁剪很好.使用放大的图像进行裁剪会将图像显示在应该是的上方.我在那里的yOffset是因为我想要的裁剪方块从scrollview所在的下方开始.
码:
CGRect rect;
float yOffset = 84;
rect.origin.x = floorf([scrollView contentOffset].x * zoomScale);
rect.origin.y = floorf(([scrollView contentOffset].y + yOffset) * zoomScale);
rect.size.width = floorf([scrollView bounds].size.width * zoomScale);
rect.size.height = floorf((320 * zoomScale));
if (rect.size.width > 320) {
rect.size.width = 320;
}
if (rect.size.height > 320) {
rect.size.height = 320;
}
CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect);
UIImage *img = imageView.image; //[UIImage imageWithCGImage:cr];
UIGraphicsBeginImageContext(rect.size);
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, 320.0f, 320.0f);
NSLog(@"drawRect: …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的UIToolBar添加一个自定义按钮(形状和颜色),但它的结果与它应该有很大不同.
什么按钮看起来像:

酒吧里的样子:

这是我用来添加它的代码:
UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTapped)];
[toolBarItems addObject:backButton];
Run Code Online (Sandbox Code Playgroud)
我到底错在了什么?
我有一个UICollectionView,里面有一些图像.在第一次向下滚动时加载图像非常慢且不稳定.之后,当图像加载时,它很好.
我想问题是图像不会被加载,直到单元格到达屏幕导致滞后.我假设我需要做的是在图像出现在屏幕上之前加载图像.我不知道该怎么做,显然是在寻找错误的东西,因为我找不到任何答案.请有人指出我正确的方向吗?
这是加载图像的代码......图像在我的应用程序中没有下载或任何东西.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell myImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我没有从远程服务器找到图像时加载默认图像:
detailImageView.image = [UIImage imageNamed:@"noimageavailable.jpg"];
Run Code Online (Sandbox Code Playgroud)
第二种选择:
detailImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"noimageavailable" ofType:@"jpg"]];
Run Code Online (Sandbox Code Playgroud)
我注意到在加载了另一个可从服务器获得的图像后,noimageavailable.jpg仍然出现在新图像后面,这意味着noimageavailable.jpg以某种方式缓存.我用两个选项imageNamed:和imageWithContentsOfFileAPI 得到了相同的结果.
这是我完成的代码:
if (detailImageURL.length == 0) {
detailImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"noimageavailable" ofType:@"jpg"]];
//Once displayed, it's cached for all lifecycle
}else{
dispatch_async(DownloadQueue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:detailImageURL]];
dispatch_async(dispatch_get_main_queue(), ^{
detailImageView.image = nil;
UIImage *image = [UIImage imageWithData:imageData];
detailImageView.image = image;
});
});
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以清除缓存?
如何根据图像的高度对NSMutableArray图像进行排序?我是否需要找到每个图像的高度并按顺序添加它,还是有更简单的方法?
我想将CCSprite转换为UIImage.我写了一个简单的测试项目.但是,我发现它不起作用.尽管图像尺寸合适,但UIImage无法在屏幕上显示.你能告诉我原因吗?
测试项目的URL如下:https://github.com/HelloVicent/ConverCCSpriteTOUIImage
非常感谢.
uiimage ×10
ios ×8
objective-c ×4
iphone ×2
xcode ×2
ccsprite ×1
cgimage ×1
core-data ×1
core-image ×1
drawrect ×1
nsdata ×1
performance ×1
uikit ×1
uitoolbar ×1
uiview ×1