Resizing UIImage to post to Twitter Sheet- iOS

青春壹個敷衍的年華 提交于 2020-01-06 04:11:27

问题


I am trying to resize my image in order to attach to twitter sheet. But I am getting error as "No known class for selector method "imageWithImage: (UIImage)image....""

- (void)twitterButtonPressed {

    UIImage *iconImage=[UIImage imageNamed:@"male_small_0.png"];
    // I am having problem in the following line
    UIImage *iconImage2=[UIImage imageWithImage:iconImage scaledToSize:CGSizeMake(73.0, 73.0)];
 }

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

回答1:


You call imageWithImage:scaledToSize: on UIImage, but your method is implemented in what I assume is your view controller. To make it work, change twitterButtonPressed to:

- (void)twitterButtonPressed {

    UIImage *iconImage=[UIImage imageNamed:@"male_small_0.png"];
    // I am having problem in the following line
    UIImage *iconImage2=[self imageWithImage:iconImage scaledToSize:CGSizeMake(73.0, 73.0)];
}

A better solution would be to create a category on UIImage with imageWithImage:scaledToSize: in it. Then, when you import this category, you don't need the method in your view controller anymore and you can leave twitterButtonPressed as-is and it'll work.



来源:https://stackoverflow.com/questions/16485895/resizing-uiimage-to-post-to-twitter-sheet-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!